#include <cstdlib>
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
while(1){
int y,m,d;
int mon[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int n,N;
cin>>y>>m>>d;
if(y==0&&m==0&&d==0) break;
if(y%400==0||(y%4==0&&y%100!=0))
{ mon[1]=29;
if(y<2000||y>9999||m>12||m<1||d<1||d>29)
cout<<-1<<endl;
else {n=(y-1)/4-((y-1)/100-(y-1)/400);
N=365*(y-1)+n;
for(int i=0;i<m-1;i++)
N+=mon[i];
N+=d;
switch(N%7)
{
case 0:cout<<7<<endl;break;
case 1:cout<<1<<endl;break;
case 2:cout<<2<<endl;break;
case 3:cout<<3<<endl;break;
case 4:cout<<4<<endl;break;
case 5:cout<<5<<endl;break;
case 6:cout<<6<<endl;break;
}}}
else{
if(y<2000||y>9999||m>12||m<1||d<1||d>28)
cout<<-1<<endl;
else{
n=(y-1)/4-((y-1)/100-(y-1)/400);
N=365*(y-1)+n;
for(int i=0;i<m-1;i++)
N+=mon[i];
N+=d;
switch(N%7)
{
case 0:cout<<7<<endl;break;
case 1:cout<<1<<endl;break;
case 2:cout<<2<<endl;break;
case 3:cout<<3<<endl;break;
case 4:cout<<4<<endl;break;
case 5:cout<<5<<endl;break;
case 6:cout<<6<<endl;break;
}}}}
system("PAUSE");
return EXIT_SUCCESS;
}