var data:string;
y:array[1..12]of integer;
numday,numday1,yy,mm,dd,day1,fullday:longint;
procedure print(yy,mm,dd:longint);
var i:longint;
sy,sm,sd:string;
begin
str(yy,sy);
str(mm,sm);
str(dd,sd);
for i:=1 to 4-length(sy) do write(0);
write(sy+'-');
for i:=1 to 2-length(sm) do write(0);
write(sm+'-');
for i:=1 to 2-length(sd) do write(0);
writeln(sd);
end;
function f(year:longint):boolean;
begin
if ((year mod 4=0) and(year mod 100<>0))or(year mod 400=0)then exit(true);
exit(false);
end;
begin
y[1]:=31;y[2]:=28;y[3]:=31;y[4]:=30;y[5]:=31;
y[6]:=30;y[7]:=31;y[8]:=31;y[9]:=30;y[10]:=31;
y[11]:=30;y[12]:=31;
readln(data);
val(copy(data,1,4),yy);
val(copy(data,6,2),mm);
val(copy(data,9,2),dd);
readln(numday);
readln(numday1);
readln(day1);
while not((numday=numday1)and(dd=day1)) do begin
if numday=1 then numday:=7 else dec(numday);
if dd=1 then begin
if mm=1 then begin mm:=12; dec(yy); end else dec(mm);
if f(yy) then begin y[2]:=29;
fullday:=y[mm];
dd:=fullday;
y[2]:=28;
end
else begin
fullday:=y[mm];
dd:=fullday;
end;
end else dec(dd);
// writeln(yy,' ',mm,' ',dd,' ',numday);
end;
print(yy,mm,dd);
close(input);
close(output);
end.