import java.util.Scanner;


public class gggg {

   public static void main(String[] args){
       System.out.println("please input year");
       Scanner input=new Scanner(System.in);
       int year=input.nextInt();
       System.out.println("please input month");
       int month=input.nextInt();
       int allDays=0;
       for(int i=1900;i<year;i++){
           if((i%4==0 && i%100!=100)||i%400==0){
               allDays+=366;
           }
           else{
               allDays+=365;
           }
       }
       Boolean isRn=false;
       if((year%4==0 && year%100!=100)||year%400==0){
           isRn=true;
       }
       int monthday=0;
       for(int i=1;i<=month;i++){
           switch(i){
           case 1:case 3:case 5:case 7:case 8:case 10:case 12:
               if(i==month){
                   monthday=31;
               }else{
                   allDays+=31;
               }
               break;
           case 2:
               if(i==month){
                   monthday=isRn?29:28;
               }
               else{
                   allDays+=isRn?29:28;
               }
               break;
           default:
               if(i==month){
                   monthday=30;
               }
               else{
                   allDays+=30;
               }
               
           }
       }
       System.out.println("日\t一\t二\t三\t四\t五\t六");
       for(int i=0;i<(allDays+1)%7;i++){
           System.out.print("\t");
       }
       for(int i=1;i<=monthday;i++){
           String temp=i+"";
           if((allDays+i)%7!=6){
               temp+="\t";
           }
           else{
               temp+="\n";
           }
           System.out.print(temp);
       }
   }
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   

}