import ru.ifmo.validate.*;

public class Validate implements Validator {
    static final int[] MONTHS = new int[]{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

    @Override
    public void validate(ValidatingScanner in) {
        int n = in.nextInt(1, 366);
        in.eoln();
        int[][] dates = new int[n][2];
        int prev = -1;
        for (int i = 0; i < n; i++) {
            int day = in.nextInt(1, 31);
            int month = in.nextInt(1, MONTHS.length) - 1;
            if (!(1 <= day && day <= MONTHS[month])) throw new RuntimeException();
            in.eoln();
            int dayOfYear = day;
            for (int j = 0; j < month; j++) {
                dayOfYear += MONTHS[j];
            }
            if (dayOfYear <= prev) throw new RuntimeException("non-increasing order " + dayOfYear);
            prev = dayOfYear;
            dates[i][0] = day;
            dates[i][1] = month;
        }
        int results = 0;
        for (int month = 0; month < MONTHS.length; month++) {
            for (int day = 1; day <= MONTHS[month]; day++) {
                if (check(dates, day, month)) {
                    results++;
                }
            }
        }
        if (results != 1) throw new RuntimeException(results + " correct solutions");
    }

    private boolean check(int[][] dates, int day, int month) {
        if (!checkFirstPhrase(dates, day, month)) {
            return false;
        }
        if (!checkSecondPhrase(dates, day, month)) {
            return false;
        }
        if (!checkThirdPhrase(dates, day, month)) {
            return false;
        }
        return true;
    }

    private boolean checkThirdPhrase(int[][] dates, int day, int month) {
        int v = 0;
        for (int m = 0; m < MONTHS.length; m++) {
            if (checkFirstPhrase(dates, day, m) && checkSecondPhrase(dates, day, m)) {
                if (m != month) return false;
                v++;
            }
        }
        return v == 1;
    }

    private boolean checkSecondPhrase(int[][] dates, int day, int month) {
        int[] countInMonth = new int[MONTHS.length];
        for (int[] date : dates) {
            countInMonth[date[1]]++;
        }
        if (countInMonth[month] < 2) return false;
        int v = 0;
        for (int[] date : dates) {
            if (date[1] == month) {
                if (checkFirstPhrase(dates, date[0], month)) {
                    if (date[0] != day) return false;
                    v++;
                }
            }
        }
        return v == 1;
    }


    private boolean checkFirstPhrase(int[][] dates, int day, int month) {
        int[] countInMonth = new int[MONTHS.length];
        boolean[] possible = new boolean[MONTHS.length];
        int possibleCount = 0;
        for (int[] date : dates) {
            countInMonth[date[1]]++;
            if (date[0] == day) {
                possible[date[1]] = true;
                possibleCount++;
            }
        }
        if (possibleCount < 2) return false;
        for (int m = 0; m < MONTHS.length; m++) {
            if (possible[m] && countInMonth[m] == 1)
                return false;
        }
        return true;
    }
}