#include<stdlib.h>
#include<stdio.h>
int main(){
// 2021 2 1
int y,m,weekday,state,totalDays,i,j,row,col,t;
state=scanf("%d%d",&y,&m);
while(state!=2||y<2000||y>2030||m<1||m>12){
fflush(stdin);
state=scanf("%d%d",&y,&m);
}
if(m==1||m==3||m==5||m==7||m==8||m==10||m==12) totalDays = 31;
else if(m==2) totalDays =((y%4==0&&y%100!=0)||y%400==0)?29:28;
else totalDays = 30;
state=scanf("%d",&weekday);
while(state!=1||weekday<0||weekday>6){
fflush(stdin);
state=scanf("%d",&weekday);
}
printf("日\t一\t二\t三\t四\t五\t六\n");
row = (weekday + totalDays)/7+1;
col = 7;t=0;
for(i=0;i<row;i++){
for(j=0;j<col;j++){
if(i==0&&j<weekday){
printf(" \t");
}else if(t<totalDays){
t += 1;
printf("%d\t",t);
}
}
printf("\n");
}
system("pause");
}