import java.util.Scanner;

/**
 * ISC Computer Science Practical Examination
 * Topic: Date, Time & Number Logic (Day-of-the-year Calculation & Leap Year Century Rule)
 * Class: DateCalculation
 */
public class DateCalculation {

    // Century Leap Year Law
    public static boolean isLeapYear(int y) {
        return (y % 400 == 0) || (y % 4 == 0 && y % 100 != 0);
    }

    // Ordinal suffix helper: 1st, 2nd, 3rd, 4th... 11th, 12th, 13th, 21st, 22nd...
    public static String getSuffix(int d) {
        if (d >= 11 && d <= 13) {
            return "TH";
        }
        switch (d % 10) {
            case 1:  return "ST";
            case 2:  return "ND";
            case 3:  return "RD";
            default: return "TH";
        }
    }

    // Helper method to convert (dayNumber, year) into formatted Date string
    public static String getDate(int dayNum, int year) {
        int[] daysInMonth = {0, 31, isLeapYear(year) ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
        String[] monthNames = {"", "JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
                               "JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER"};

        int d = dayNum;
        int m = 1;
        while (d > daysInMonth[m]) {
            d -= daysInMonth[m];
            m++;
        }
        return d + getSuffix(d) + " " + monthNames[m] + ", " + year;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        // Accept Inputs
        System.out.print("DAY NUMBER: ");
        int dayNumber = in.nextInt();

        System.out.print("YEAR: ");
        int year = in.nextInt();

        System.out.print("DATE AFTER (N DAYS): ");
        int n = in.nextInt();

        // 1. Validate Year
        if (year < 1000 || year > 9999) {
            System.out.println("INVALID YEAR");
            return;
        }

        // 2. Validate Day Number according to Leap Year status
        boolean isLeap = isLeapYear(year);
        int maxDays = isLeap ? 366 : 365;

        if (dayNumber < 1 || dayNumber > maxDays) {
            System.out.println("INVALID DAY NUMBER");
            return;
        }

        // 3. Validate N (1 <= N <= 100)
        if (n < 1 || n > 100) {
            System.out.println("INVALID NUMBER OF DAYS");
            return;
        }

        // 4. Compute Date corresponding to Day Number
        String currentDate = getDate(dayNumber, year);

        // 5. Compute Date after N days (handling year boundary crossover)
        int targetDay = dayNumber + n;
        int targetYear = year;
        int targetMaxDays = isLeapYear(targetYear) ? 366 : 365;

        if (targetDay > targetMaxDays) {
            targetDay -= targetMaxDays;
            targetYear++;
        }
        String futureDate = getDate(targetDay, targetYear);

        // Display Outputs
        System.out.println("\nOUTPUT:");
        System.out.println("DATE: " + currentDate);
        System.out.println("DATE AFTER " + n + " DAYS: " + futureDate);
    }
}
