{
TASK:KALENDORIUS
LANG:PASCAL
}
program senas_kalendorius;

  type metai = 1900..2100;

   function kel (m : metai) : boolean;
      { ar metai m keliamieji }
    begin
      kel := (m mod 400 = 0) or
             (m mod 100 <> 0) and
             (m mod 4 = 0)
    end;

    function kam_tinka (sm: metai; psmd: integer): metai;
      var rasta, kel_sm, kel_nm: boolean;
          nm, pnmd: integer;
    begin
      rasta := false;
      kel_sm := kel(sm); { ar duoti metai keliamieji }
      nm := 2005; { tinkamø metø pradësime ieðkoti nuo 2005 }
      pnmd := 6; { 2005 metu pirmoji diena - ðeðtadienis }
      kel_nm := false; { 2005 metai - nekeliamieji }
      while not rasta do
       if (kel_nm = kel_sm) and (pnmd = psmd)
          then rasta := true
          else begin
                 if kel_nm
                    then pnmd := pnmd + 2
                    else pnmd := pnmd + 1;
                 if pnmd > 7
                    then pnmd := pnmd - 7;
                 nm := nm + 1;
                 kel_nm := kel (nm);
               end;
      kam_tinka := nm;
    end; { kam_tinka }

  var sm, nm, psmd: integer;
begin
  readln (sm, psmd);
  nm := kam_tinka (sm, psmd);
  writeln (nm);
end.