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 = 3004;
        var bd1 = new DateTime(y1, m1, d1);
        var bd2 = new DateTime(y2, m2, d2);
        var start = new DateTime(yyyy, 1, 1);
        for (int i = 0; i <= 365; i++)
        {
            var now = start.AddDays(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;
    }
}