uses wincrt;

type tdate = record
       year: longint;
       month: integer;
       day: integer;
     end;

const days: array[0..6] of string = (
      'Sunday',
      'Monday',
      'Tuesday',
      'Wedensday',
      'Thursday',
      'Friday',
      'Saturday');

const months: array[1..12] of integer = (
      31,28,31,30,31,30,31,31,30,31,30,31);

function isleapyear(year:longint):boolean;
  begin
    isleapyear := (((year mod 4=0) and not (year mod 100=0)) or (year mod 400=0)) and not (year mod 4000 = 0);
  end;

function daysinyear(year:longint):integer;
  begin
    if isleapyear(year) then daysinyear := 366 else daysinyear := 365;
  end;

function daysinmonth(year:longint; month:integer):integer;
  var days:integer;
  begin
    days := months[month];
    if (month=2) and isleapyear(year) then inc(days);
    daysinmonth := days;
  end;

procedure swapdates(var date1:tdate; var date2:tdate);
  var date:tdate;
  begin
    date  := date1;
    date1 := date2;
    date2 := date;
  end;

function greaterdate(date1,date2:tdate):boolean;
  begin
    greaterdate := (date1.year>date2.year) or
                   ((date1.year=date2.year) and (date1.month>date2.month)) or
                   ((date1.year=date2.year) and (date1.month=date2.month) and (date1.day>date2.day));
  end;

function subtractdays(date1,date2:tdate):longint;
  var days,i,j,k:longint;
      multiplier:integer;
  begin
    if greaterdate(date2,date1) then
      begin
        swapdates(date2,date1);
        multiplier := -1;
      end
    else
      multiplier := 1;

    days := ((date1.year-date2.year) div 4000)*(969*366+3031*365); {number of days after 4000 years}
    for i := date2.year to date2.year + (date1.year-date2.year)mod 4000 - 1 do
      inc(days,daysinyear(i)); 
    for i := 1 to date2.month - 1 do
      dec(days,daysinmonth(date2.year,i));
    dec(days,date2.day);
    for i := 1 to date1.month - 1 do
      inc(days,daysinmonth(date1.year,i));
    inc(days,date1.day);
    subtractdays := days*multiplier;
  end;

function stringdate(date:tdate):string;
  var s1,s2: string;
  begin
    str(date.month,s2); s1 := s2+'/';
    str(date.day,s2);   s1 := s1 + s2 + '/';
    str(date.year,s2);  s1 := s1 + s2;
    stringdate := s1;
  end;

var date1,date2:tdate;
    daynumber:integer;

begin
  date1.year  := 1999;
  date1.month := 3;
  date1.day   := 27;
  date2.day   := 1;

  write('  Enter month number:   '); readln(date2.month);
  write('  Enter year:           '); readln(date2.year);
  writeln;

  daynumber := ((6+subtractdays(date2,date1)) mod 7);
  if daynumber<0 then inc(daynumber,7);

  writeln('  ', stringdate(date2),' is a ',days[daynumber]);
end.