Problem:
Age Difference

A trick of fate caused Hatsumi and Taku to come to know each other. To keep the encounter in memory, they decided to calculate the difference between their ages. But the difference in ages varies depending on the day it is calculated. While trying again and again, they came to notice that the difference of their ages will hit a maximum value even though the months move on forever.

Given the birthdays for the two, make a program to report the maximum difference between their ages. The age increases by one at the moment the birthday begins. If the birthday coincides with the 29th of February in a leap year, the age increases at the moment the 1st of March arrives in non-leap years.

Input

The input is given in the following format.

y_1 m_1 d_1
y_2 m_2 d_2

The first and second lines provide Hatsumi’s and Taku’s birthdays respectively in year y_i (1 ≤ y_i ≤ 3000), month m_i (1 ≤ m_i ≤ 12), and day d_i (1 ≤ d_i ≤ Dmax) format. Where Dmax is given as follows:

28 when February in a non-leap year

29 when February in a leap-year

30 in April, June, September, and November

31 otherwise.

It is a leap year if the year represented as a four-digit number is divisible by 4. Note, however, that it is a non-leap year if divisible by 100, and a leap year if divisible by 400.

Output

Output the maximum difference between their ages.

Sample Input 1

1999 9 9
2001 11 3

Sample Output 1

3

In this example, the difference of ages between them in 2002 and subsequent years is 3 on the 1st of October, but 2 on the 1st of December.

Sample Input 2

2008 2 29
2015 3 1

Sample Output 2

8

In this example, the difference of ages will become 8 on the 29th of February in and later years than 2016.

Solution in C# (judge verdict: Accepted)

using static System.Math;
using System;

public class hello
{
    public static void Main()
    {
        string[] line = Console.ReadLine().Trim().Split(' ');
        var y1 = int.Parse(line[0]);
        var m1 = int.Parse(line[1]);
        var d1 = int.Parse(line[2]);
        line = Console.ReadLine().Trim().Split(' ');
        var y2 = int.Parse(line[0]);
        var m2 = int.Parse(line[1]);
        var d2 = int.Parse(line[2]);
        var ans = getAge(y1, m1, d1, y2, m2, d2);
        Console.WriteLine(ans);
    }
    public static int getAge(int y1, int m1, int d1, int y2, int m2, int d2)
    {
        var ans = -1;
        var yyyy = 2008;
        var bd1 = new DateTime(y1, m1, d1);
        var bd2 = new DateTime(y2, m2, d2);
        var mo = new int[] { 1, 3, 5, 7, 8, 10, 12 };
        foreach (var x in mo)
        {
            for (int i = 1; i <= 31; i++)
            {
                var now = new DateTime(yyyy, x, i);
                var age1 = now.Year - bd1.Year;
                if (bd1 > now.AddYears(-age1)) age1--;
                var age2 = now.Year - bd2.Year;
                if (bd2 > now.AddYears(-age2)) age2--;
                ans = Max(ans, Abs(age1 - age2));
            }
        }
        var mo2 = new int[] { 4, 6, 9, 11 };
        foreach (var x in mo2)
        {
            for (int i = 1; i <= 30; i++)
            {
                var now = new DateTime(yyyy, x, i);
                var age1 = now.Year - bd1.Year;
                if (bd1 > now.AddYears(-age1)) age1--;
                var age2 = now.Year - bd2.Year;
                if (bd2 > now.AddYears(-age2)) age2--;
                ans = Max(ans, Abs(age1 - age2));
            }
        }
        for (int i = 1; i <= 29; i++)
        {
            var now = new DateTime(yyyy, 2, i);
            var age1 = now.Year - bd1.Year;
            if (bd1 > now.AddYears(-age1)) age1--;
            var age2 = now.Year - bd2.Year;
            if (bd2 > now.AddYears(-age2)) age2--;
            ans = Max(ans, Abs(age1 - age2));
        }
        return ans;
    }
}