using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using System.Diagnostics;
using System.Numerics;
using static System.Console;
using static System.Convert;
using static System.Math;
using static Template;
using Pi = Pair<int, int>;

class Solver
{
    public void Solve(Scanner sc)
    {
        int A, B, C, D;
        sc.Make(out A, out B, out C, out D);
        var res = 0L;
        for (int z = C; z <= D; z++)
        {
            long l = Min(B, z - C);
           long r = Min(B, z - B)+1;
            res += Max(0, (Min(r, A) - l)) * (B - A + 1);
            chmax(ref l, A);
            chmax(ref r, A);
            res += (B - l) * (B - l + 1L) / 2 - (B - r) * (B - r+1) / 2;
        }
        Console.WriteLine(res);
    }
}

#region Template
public static class Template
{
    static void Main(string[] args)
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Solver().Solve(new Scanner());
        Console.Out.Flush();
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) > 0) { a = b; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void swap<T>(this IList<T> A, int i, int j) { var t = A[i]; A[i] = A[j]; A[j] = t; }
    public static T[] Create<T>(int n, Func<T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(); return rt; }
    public static T[] Create<T>(int n, Func<int, T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(i); return rt; }
    public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> A) => A.OrderBy(v => Guid.NewGuid());
    public static int CompareTo<T>(this T[] A, T[] B, Comparison<T> cmp = null) { cmp = cmp ?? Comparer<T>.Default.Compare; for (var i = 0; i < Min(A.Length, B.Length); i++) { int c = cmp(A[i], B[i]); if (c > 0) return 1; else if (c < 0) return -1; } if (A.Length == B.Length) return 0; if (A.Length > B.Length) return 1; else return -1; }
    public static int MaxElement<T>(this IList<T> A, Comparison<T> cmp = null) { cmp = cmp ?? Comparer<T>.Default.Compare; T max = A[0]; int rt = 0; for (int i = 1; i < A.Count; i++) if (cmp(max, A[i]) < 0) { max = A[i]; rt = i; } return rt; }
    public static T PopBack<T>(this List<T> A) { var v = A[A.Count - 1]; A.RemoveAt(A.Count - 1); return v; }
    public static void Fail<T>(T s) { Console.WriteLine(s); Console.Out.Close(); Environment.Exit(0); }
}
public class Scanner
{
    public string Str => Console.ReadLine().Trim();
    public int Int => int.Parse(Str);
    public long Long => long.Parse(Str);
    public double Double => double.Parse(Str);
    public int[] ArrInt => Str.Split(' ').Select(int.Parse).ToArray();
    public long[] ArrLong => Str.Split(' ').Select(long.Parse).ToArray();
    public char[][] Grid(int n) => Create(n, () => Str.ToCharArray());
    public int[] ArrInt1D(int n) => Create(n, () => Int);
    public long[] ArrLong1D(int n) => Create(n, () => Long);
    public int[][] ArrInt2D(int n) => Create(n, () => ArrInt);
    public long[][] ArrLong2D(int n) => Create(n, () => ArrLong);
    private Queue<string> q = new Queue<string>();
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public T Next<T>() { if (q.Count == 0) foreach (var item in Str.Split(' ')) q.Enqueue(item); return (T)Convert.ChangeType(q.Dequeue(), typeof(T)); }
    public void Make<T1>(out T1 v1) => v1 = Next<T1>();
    public void Make<T1, T2>(out T1 v1, out T2 v2) { v1 = Next<T1>(); v2 = Next<T2>(); }
    public void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3) { Make(out v1, out v2); v3 = Next<T3>(); }
    public void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4) { Make(out v1, out v2, out v3); v4 = Next<T4>(); }
    public void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5) { Make(out v1, out v2, out v3, out v4); v5 = Next<T5>(); }
    public void Make<T1, T2, T3, T4, T5, T6>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6) { Make(out v1, out v2, out v3, out v4, out v5); v6 = Next<T6>(); }
    public void Make<T1, T2, T3, T4, T5, T6, T7>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6, out T7 v7) { Make(out v1, out v2, out v3, out v4, out v5, out v6); v7 = Next<T7>(); }
    //public (T1, T2) Make<T1, T2>() { Make(out T1 v1, out T2 v2); return (v1, v2); }
    //public (T1, T2, T3) Make<T1, T2, T3>() { Make(out T1 v1, out T2 v2, out T3 v3); return (v1, v2, v3); }
    //public (T1, T2, T3, T4) Make<T1, T2, T3, T4>() { Make(out T1 v1, out T2 v2, out T3 v3, out T4 v4); return (v1, v2, v3, v4); }
}
public class Pair<T1, T2> : IComparable<Pair<T1, T2>>
{
    public T1 v1;
    public T2 v2;
    public Pair() { }
    public Pair(T1 v1, T2 v2)
    { this.v1 = v1; this.v2 = v2; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int CompareTo(Pair<T1, T2> p)
    {
        var c = Comparer<T1>.Default.Compare(v1, p.v1);
        if (c == 0)
            c = Comparer<T2>.Default.Compare(v2, p.v2);
        return c;
    }
    public override string ToString() => $"{v1.ToString()} {v2.ToString()}";
    public void Deconstruct(out T1 a, out T2 b) { a = v1; b = v2; }
}

public class Pair<T1, T2, T3> : Pair<T1, T2>, IComparable<Pair<T1, T2, T3>>
{
    public T3 v3;
    public Pair() : base() { }
    public Pair(T1 v1, T2 v2, T3 v3) : base(v1, v2)
    { this.v3 = v3; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int CompareTo(Pair<T1, T2, T3> p)
    {
        var c = base.CompareTo(p);
        if (c == 0)
            c = Comparer<T3>.Default.Compare(v3, p.v3);
        return c;
    }
    public override string ToString() => $"{base.ToString()} {v3.ToString()}";
    public void Deconstruct(out T1 a, out T2 b, out T3 c) { Deconstruct(out a, out b); c = v3; }
}
#endregion


using CodeforcesRound643Div2.Questions;
using CodeforcesRound643Div2.Extensions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CodeforcesRound643Div2.Questions
{
    public class QuestionC_Review : AtCoderQuestionBase
    {
        public override IEnumerable<object> Solve(TextReader inputStream)
        {
            var (a, b, c, d) = inputStream.ReadValue<int, int, int, int>();
            var xyCount = new long[1000002];
            for (int x = a; x <= b; x++)
            {
                xyCount[x + b] += 1;
                xyCount[x + c + 1] -= 1;
            }

            for (int prefixSum = 0; prefixSum < 2; prefixSum++)
            {
                for (int i = 1; i < xyCount.Length; i++)
                {
                    xyCount[i] += xyCount[i - 1];
                }
            }

            long count = 0;
            for (int z = c; z <= d; z++)
            {
                count += xyCount[xyCount.Length - 1] - xyCount[z];
            }

            yield return count;
        }
    }
}

namespace CodeforcesRound643Div2
{
    class Program
    {
        static void Main(string[] args)
        {
            IAtCoderQuestion question = new QuestionC_Review();    // 問題に合わせて書き換え
            var answers = question.Solve(Console.In);

            var writer = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(writer);
            foreach (var answer in answers)
            {
                Console.WriteLine(answer);
            }
            Console.Out.Flush();
        }
    }
}

#region Base Class

namespace CodeforcesRound643Div2.Questions
{

    public interface IAtCoderQuestion
    {
        IEnumerable<object> Solve(string input);
        IEnumerable<object> Solve(TextReader inputStream);
    }

    public abstract class AtCoderQuestionBase : IAtCoderQuestion
    {
        public IEnumerable<object> Solve(string input)
        {
            var stream = new MemoryStream(Encoding.Unicode.GetBytes(input));
            var reader = new StreamReader(stream, Encoding.Unicode);

            return Solve(reader);
        }

        public abstract IEnumerable<object> Solve(TextReader inputStream);
    }
}

#endregion

#region Extensions

namespace CodeforcesRound643Div2.Extensions
{
    internal static class TextReaderExtensions
    {
        internal static int ReadInt(this TextReader reader) => int.Parse(ReadString(reader));
        internal static long ReadLong(this TextReader reader) => long.Parse(ReadString(reader));
        internal static double ReadDouble(this TextReader reader) => double.Parse(ReadString(reader));
        internal static string ReadString(this TextReader reader) => reader.ReadLine();

        internal static int[] ReadIntArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(int.Parse).ToArray();
        internal static long[] ReadLongArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(long.Parse).ToArray();
        internal static double[] ReadDoubleArray(this TextReader reader, char separator = ' ') => ReadStringArray(reader, separator).Select(double.Parse).ToArray();
        internal static string[] ReadStringArray(this TextReader reader, char separator = ' ') => reader.ReadLine().Split(separator);

        // Supports primitive type only.
        internal static T1 ReadValue<T1>(this TextReader reader) => (T1)Convert.ChangeType(reader.ReadLine(), typeof(T1));

        internal static (T1, T2) ReadValue<T1, T2>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            return (v1, v2);
        }

        internal static (T1, T2, T3) ReadValue<T1, T2, T3>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            return (v1, v2, v3);
        }

        internal static (T1, T2, T3, T4) ReadValue<T1, T2, T3, T4>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            return (v1, v2, v3, v4);
        }

        internal static (T1, T2, T3, T4, T5) ReadValue<T1, T2, T3, T4, T5>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            return (v1, v2, v3, v4, v5);
        }

        internal static (T1, T2, T3, T4, T5, T6) ReadValue<T1, T2, T3, T4, T5, T6>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            return (v1, v2, v3, v4, v5, v6);
        }

        internal static (T1, T2, T3, T4, T5, T6, T7) ReadValue<T1, T2, T3, T4, T5, T6, T7>(this TextReader reader, char separator = ' ')
        {
            var inputs = ReadStringArray(reader, separator);
            var v1 = (T1)Convert.ChangeType(inputs[0], typeof(T1));
            var v2 = (T2)Convert.ChangeType(inputs[1], typeof(T2));
            var v3 = (T3)Convert.ChangeType(inputs[2], typeof(T3));
            var v4 = (T4)Convert.ChangeType(inputs[3], typeof(T4));
            var v5 = (T5)Convert.ChangeType(inputs[4], typeof(T5));
            var v6 = (T6)Convert.ChangeType(inputs[5], typeof(T6));
            var v7 = (T7)Convert.ChangeType(inputs[6], typeof(T7));
            return (v1, v2, v3, v4, v5, v6, v7);
        }
    }
}

#endregion


using System;
using System.IO;
using System.Text;

namespace Cubical_Planet
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            int x1 = Next(), y1 = Next(), z1 = Next();
            int x2 = Next(), y2 = Next(), z2 = Next();

            writer.WriteLine(x1 == x2 || y1 == y2 || z1 == z2 ? "YES" : "NO");
            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Roman_and_Numbers
{
    internal class Program
    {
        private static readonly StreamReader reader =
            new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);

        private static readonly StreamWriter writer =
            new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static void Main(string[] args)
        {
            string[] ss = reader.ReadLine().Split(' ');
            long n = long.Parse(ss[0]);
            int m = int.Parse(ss[1]);

            var digits = new List<int>();
            while (n > 0)
            {
                digits.Add((int) (n%10));
                n /= 10;
            }

            int nn = digits.Count;
            int max = 1 << nn;
            var dp = new long[max,m];

            for (int i = 0; i < nn; i++)
            {
                if (digits[i] != 0)
                    dp[1 << i, digits[i]%m] += 1;
            }

            for (int i = 1; i < max; i++)
            {
                for (int j = 0; j < m; j++)
                {
                    if (dp[i, j] != 0)
                    {
                        for (int k = 0; k < nn; k++)
                        {
                            if ((i & (1 << k)) == 0)
                            {
                                dp[i | (1 << k), (j*10 + digits[k])%m] += dp[i, j];
                            }
                        }
                    }
                }
            }
            long ans = dp[max - 1, 0];

            int[] counts = new int[10];
            foreach (var digit in digits)
            {
                counts[digit]++;
            }

            for (int i = 0; i < 10; i++)
            {
                if (counts[i]!=0)
                {
                    for (int j = counts[i]; j>1; j--)
                    {
                        ans /= j;
                    }
                }
            }

            writer.WriteLine(ans);
            writer.Flush();
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace Artem_and_Array
{
    internal class Program
    {
        private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024*10), Encoding.ASCII, false, 1024*10);
        private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024*10), Encoding.ASCII, 1024*10);

        private static int[] nn;

        private static void Main(string[] args)
        {
            int n = Next();
            nn = new int[n + 2];
            nn[0] = -10000000;
            for (int i = 1; i <= n; i++)
            {
                nn[i] = Next();
            }
            nn[n + 1] = -10000000;

            long count = 0;

            var points = new point[n + 2];
            var q = new Queue<point>();
            for (int i = 1; i <= n; i++)
            {
                var p = new point(i - 1, i + 1, nn[i]);
                points[i] = p;
                if (p.value >= p.current)
                {
                    q.Enqueue(p);
                    p.used = true;
                }
            }

            while (q.Count > 0)
            {
                point p = q.Dequeue();
                count += p.value;

                point p1 = points[p.left];
                point p2 = points[p.right];
                p1.right = p.right;
                p2.left = p.left;
                p1.Recalc();
                p2.Recalc();

                if (p1.value >= p1.current && !p1.used)
                {
                    q.Enqueue(p1);
                    p1.used = true;
                }
                if (p2.value >= p2.current && !p2.used)
                {
                    q.Enqueue(p2);
                    p2.used = true;
                }
            }

            var heap = new MaxBinaryHeapObject(new point(0, 0, 0));
            for (int i = 1; i <= n; i++)
            {
                if (!points[i].used)
                    heap.Add(points[i]);
            }

            while (heap.HeapSize > 2)
            {
                point p = heap.GetMax();
                count += p.value;

                point p1 = points[p.left];
                point p2 = points[p.right];
                p1.right = p.right;
                p2.left = p.left;

                p1.Recalc();
                heap.Heapify(p1);
                p2.Recalc();
                heap.Heapify(p2);
            }

            writer.WriteLine(count);
            writer.Flush();
        }

        private static int Next()
        {
            int c;
            int res = 0;
            do
            {
                c = reader.Read();
                if (c == -1)
                    return res;
            } while (c < '0' || c > '9');
            res = c - '0';
            while (true)
            {
                c = reader.Read();
                if (c < '0' || c > '9')
                    return res;
                res *= 10;
                res += c - '0';
            }
        }

        #region Nested type: MaxBinaryHeapObject

        public class MaxBinaryHeapObject
        {
            private readonly IComparer<point> _comparer;
            private readonly List<point> _list;

            public MaxBinaryHeapObject(IComparer<point> comparer)
            {
                _comparer = comparer ?? Comparer<point>.Default;
                _list = new List<point>();
            }

            public int HeapSize
            {
                get { return _list.Count; }
            }

            public void Add(point value)
            {
                _list.Add(value);
                value.where = _list.Count - 1;
                MoveUp(HeapSize - 1);
            }

            private void MoveUp(int i)
            {
                int parent = (i - 1)/2;

                while (i > 0 && _comparer.Compare(_list[parent], _list[i]) == -1)
                {
                    point temp = _list[i];
                    _list[i] = _list[parent];
                    _list[parent] = temp;

                    _list[i].where = i;
                    _list[parent].where = parent;

                    i = parent;
                    parent = (i - 1)/2;
                }
            }

            public void Heapify(point t)
            {
                MoveUp(t.where);
                MoveDown(t.where);
            }

            private void MoveDown(int i)
            {
                for (;;)
                {
                    int leftChild = 2*i + 1;
                    int rightChild = 2*i + 2;
                    int largestChild = i;

                    if (leftChild < HeapSize && _comparer.Compare(_list[leftChild], _list[largestChild]) == 1)
                    {
                        largestChild = leftChild;
                    }

                    if (rightChild < HeapSize && _comparer.Compare(_list[rightChild], _list[largestChild]) == 1)
                    {
                        largestChild = rightChild;
                    }

                    if (largestChild == i)
                    {
                        break;
                    }

                    point temp = _list[i];
                    _list[i] = _list[largestChild];
                    _list[largestChild] = temp;

                    _list[i].where = i;
                    _list[largestChild].where = largestChild;

                    i = largestChild;
                }
            }

            public point Max()
            {
                return _list[0];
            }

            public point GetMax()
            {
                point result = _list[0];
                _list[0] = _list[HeapSize - 1];
                _list[0].where = 0;
                _list.RemoveAt(HeapSize - 1);
                MoveDown(0);
                return result;
            }
        }

        #endregion

        #region Nested type: point

        public class point : IComparer<point>
        {
            public readonly int current;
            public int left;
            public int right;
            public bool used;
            public int value;
            public int where;

            public point(int left, int right, int current)
            {
                this.left = left;
                this.right = right;
                this.current = current;
                value = Math.Min(nn[left], nn[right]);
            }

            #region IComparer<point> Members

            public int Compare(point x, point y)
            {
                int c = (x.value).CompareTo(y.value);
                //if (c == 0)
                //    c = y._current.CompareTo(x._current);
                return c;
            }

            #endregion

            public void Recalc()
            {
                value = Math.Min(nn[left], nn[right]);
            }
        }

        #endregion
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CodeForces800
{
    internal static class Program
    {
        public static void Main()
        {
            // var strrep = Console.ReadLine().Split(' ').Select(x => Int32.Parse(x)).ToList();
            // var rep = strrep[0];
            ulong p = 998244353;
            var primes = new HashSet<int>();
            var s = 2;
            primes.Add(2);
            for (var i = 2; i < 1000; i++)
            {
                if (s * s < i)
                    s++;
                if (!Enumerable.Range(2, Math.Max(s - 1, 0)).Any(x => i % x == 0))
                {
                    primes.Add(i);
                }
            }
            for (var iter = 0; iter < 1; iter++)
            {
                var strrep = Console.ReadLine().Split(' ').Select(x => ulong.Parse(x)).ToList();
                var n = strrep[0];
                var m = strrep[1];
                long all = 0;
                ulong curgcd = 1;
                ulong prev = m % p;
                ulong prevbad = m % p;
                var f = true;
                for (ulong i = 2; i <= n; i++)
                {
                    prev *= (m % p);
                    prev %= p;
                    if (!f)
                    {
                        all += (long)prev;
                        all %= (long)p;
                        continue;
                    }
                    if(primes.Contains((int)i))
                        curgcd = curgcd * i;
                    if (m < curgcd)
                        f = false;
                    prevbad *= ((m / curgcd) % p);
                    prevbad %= p;
                    all += ((long)prev - (long)prevbad);
                    all %= (long)p;
                    all += (long)p;
                    all %= (long)p;
                }
                Console.WriteLine(all);
            }
        }

        private static ulong GCD(ulong a, ulong b)
        {
            while (a != 0 && b != 0)
            {
                if (a > b)
                    a %= b;
                else
                    b %= a;
            }

            return a | b;
        }
    }
}


using System;
using System.Linq;

namespace CodeForces
{
    class Program
    {
        static void Main(string[] args)
        {
            var arr = Console.ReadLine().Split(' ');
            var n = Int32.Parse(arr[0]);
            var m = long.Parse(arr[1]);
            var res = GetRes(n, m);
            Console.WriteLine(res);

            // Test();
        }

        const int MOD_VAL = 998244353;
        private static int GetRes(int n, long m)
        {
            if (n == 1) return 0;
            var fullCount = GetFullCount(n, m);
            var noOk = GetNOKCount(n,m);
            var res = fullCount - noOk;
            if(res < 0) res+=MOD_VAL;
            return res;
        }

        private static int GetFullCount(int n, long m)
        {
            var divisor = (int)((m-1) % MOD_VAL);
            if (divisor == 0) return n;
            var reverse = GetReverse(divisor);
            var mAfterMod = (int)(m % MOD_VAL);
            var res = (int)(((long)GetPow(mAfterMod, n+1) - mAfterMod) *
                reverse % MOD_VAL);
            if (res < 0) res+=MOD_VAL;
            return res;
        }

        private static int GetReverse(int val)
        {
            var (a,b) = ExGcd(MOD_VAL, val);
            return b >= 0 ? (int) b: (int)(MOD_VAL+b);
        }

        private static (long, long) ExGcd(long num0, long num1)
        {
            if (num0 % num1 == 0)
            {
                return (0, 1);
            }
            // a0 * num0 + b0 * num1 = num2
            var num2 = num0 % num1;
            var a0 = 1;
            var b0 = -(num0 / num1);
            // a1 * num1 + b1 * num2 = gcd
            var (a1, b1) = ExGcd(num1, num2);
            // (a0*b1) * num0 + (a1 + b0*b1) * num1 = gcd
            return (a0 * b1, a1 + b0 * b1);
        }

        private static int GetPow(int baseVal, int exponent)
        {
            var index = 0;
            var res = 1L;
            long tmp = baseVal;
            while(1<<index <= exponent)
            {
                if ((1<<index & exponent)!=0)
                {
                    res = res * tmp % MOD_VAL;
                }
                tmp = tmp * tmp % MOD_VAL;
                index++;
            }
            return (int)res;
        }

        private static int GetNOKCount(int n, long m)
        {
            var primes = new int[]
            {
                2,3,5,7,11,13,17,19,23,29,31,37
            };
            var product = 1L;
            var noOkI = m % MOD_VAL;
            var noOk = noOkI;
            for(var i =2;i<=n;i++)
            {
                if(primes.Contains(i))
                {
                    product*=i;
                }
                if( m < product)
                {
                    break;
                }
                noOkI = noOkI * (m/product % MOD_VAL) % MOD_VAL;
                noOk = (noOk + noOkI) % MOD_VAL;
            }
            return (int)noOk;
        }

        private static (int,int) GetTestData(Random random)
        {
            return (random.Next(10)+3, random.Next(4));
        }

    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Main {
    class Program {

        static int[] primes;

        void solve() {
            bool[] p = new bool[1000];
            int count = p.Length - 2;
            for (int i = 2; i * i < p.Length; i++) {
                if (!p[i]) {
                    for (int j = i * i; j < p.Length; j += i) {
                        if (!p[j]) {
                            p[j] = true;
                            --count;
                        }
                    }
                }
            }
            primes = new int[count];
            for (int i = 2, j = 0; i < p.Length; i++) {
                if (!p[i]) {
                    primes[j++] = i;
                }
            }
            int n = sc.nextInt();
            if (n == 1) {
                output.WriteLine(1);
            } else {
                output.WriteLine(go(0, n, 100, 1));
            }
        }

        static long go(int x, int n, int last, long cur) {
            if (n == 1) {
                return cur;
            }
            long ret = Int64.MaxValue;
            for (int i = 1; i <= last; i++) {
                if (cur >= Int64.MaxValue / primes[x]) {
                    break;
                }
                cur *= primes[x];
                if (n % (i + 1) == 0) {
                    ret = Math.Min(ret, go(x + 1, n / (i + 1), i, cur));
                }
            }
            return ret;
        }

        public abstract class InputReader {
            protected char[] buf;
            protected int current;
            protected bool isFinished;

            public InputReader() {
                buf = new char[1024];
                current = 1024;
            }

            public abstract int read();

            public int nextInt() {
                int c = read();
                while (c <= 32 && c >= 0) {
                    c = read();
                }
                int sgn = 1;
                if (c == '-') {
                    sgn = -1;
                    c = read();
                }
                if (c < '0' || c > '9') {
                    throw new Exception("digit expected " + (char)c
                            + " found");
                }
                int ret = 0;
                while (c >= '0' && c <= '9') {
                    ret = ret * 10 + (c - '0');
                    c = read();
                }
                if (c == -1) {
                    isFinished = true;
                }
                if (c > 32) {
                    throw new Exception("space character expected "
                            + (char)c + " found");
                }
                return ret * sgn;
            }

            public long nextLong() {
                int c = read();
                while (c <= 32 && c >= 0) {
                    c = read();
                }
                int sgn = 1;
                if (c == '-') {
                    sgn = -1;
                    c = read();
                }
                if (c < '0' || c > '9') {
                    throw new Exception("digit expected " + (char)c
                            + " found");
                }
                long ret = 0;
                while (c >= '0' && c <= '9') {
                    ret = ret * 10 + (c - '0');
                    c = read();
                }
                if (c == -1) {
                    isFinished = true;
                }
                if (c > 32) {
                    throw new Exception("space character expected "
                            + (char)c + " found");
                }
                return ret * sgn;
            }

            public string next() {
                int c = read();
                while (c <= 32 && c >= 0) {
                    c = read();
                }
                StringBuilder sb = new StringBuilder();
                while (c > 32) {
                    sb.Append((char)(c));
                    c = read();
                }
                if (c == -1) {
                    isFinished = true;
                }
                return sb.ToString();
            }

            public bool isFinishedFunction() {
                return isFinished;
            }

        }

        public class StreamInputReader : InputReader {
            private StreamReader stream;

            public StreamInputReader(StreamReader stream)
                : base() {
                this.stream = stream;
            }

            override public int read() {
                if (current == buf.Length) {
                    if (stream.ReadBlock(buf, 0, buf.Length) == 0) {
                        return -1;
                    }
                    current = 0;
                }
                return buf[current++];
            }
        }

        InputReader sc;
        StreamWriter output;

        public static void Main(string[] args) {
            new Program().run();
        }

        public void run() {
            sc = new StreamInputReader(new StreamReader(Console.OpenStandardInput()));
            output = new StreamWriter(Console.OpenStandardOutput());
            solve();
            output.Close();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Text;

namespace InnaAndPony
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] nmijab = Console.ReadLine().Split(' ');
            int n = Convert.ToInt32(nmijab[0]);
            int m = Convert.ToInt32(nmijab[1]);
            int i = Convert.ToInt32(nmijab[2]);
            int j = Convert.ToInt32(nmijab[3]);
            int a = Convert.ToInt32(nmijab[4]);
            int b = Convert.ToInt32(nmijab[5]);
            int k1 = 0;
            int k2 = 0;
            int k3 = 0;
            int k4 = 0;
            int k = 0;

            if (((i == 1) && (j == 1)) || ((i == n) && (j == 1)) || ((i == n) && (j == m)) || ((i == 1) && (j == m)))
            {
                Console.WriteLine(0);
            }
            else
            {
                if (((i - 1 >= a) || (n - i >= a)) && ((j - 1 >= b) || (m - j >= b)))
                {
                    if ((Math.Abs(i - 1) % a == 0) && (Math.Abs(j - 1) % b == 0) && ((Math.Abs(i - 1) / a + Math.Abs(j - 1) / b) % 2 == 0))
                    {
                        k1 = Math.Max(Math.Abs(i - 1) / a, Math.Abs(j - 1) / b);
                        //       Console.WriteLine(k1);
                    }
                    else
                    {
                        k1 = 1000000000;
                        //       Console.WriteLine(k1);
                    }

                    if ((Math.Abs(i - n) % a == 0) && (Math.Abs(j - 1) % b == 0) && ((Math.Abs(i - n) / a + Math.Abs(j - 1) / b) % 2 == 0))
                    {
                        k2 = Math.Max(Math.Abs(i - n) / a, Math.Abs(j - 1) / b);
                        //        Console.WriteLine(k2);
                    }
                    else
                    {
                        k2 = 1000000000;
                        //     Console.WriteLine(k2);
                    }

                    if ((Math.Abs(i - n) % a == 0) && (Math.Abs(j - m) % b == 0) && ((Math.Abs(i - n) / a + Math.Abs(j - m) / b) % 2 == 0))
                    {
                        k3 = Math.Max(Math.Abs(i - n) / a, Math.Abs(j - m) / b);
                        //   Console.WriteLine(k3);
                    }
                    else
                    {
                        k3 = 1000000000;
                        //       Console.WriteLine(k3);
                    }

                    if ((Math.Abs(i - 1) % a == 0) && (Math.Abs(j - m) % b == 0) && ((Math.Abs(i - 1) / a + Math.Abs(j - m) / b) % 2 == 0))
                    {
                        k4 = Math.Max(Math.Abs(i - 1) / a, Math.Abs(j - m) / b);
                        //          Console.WriteLine(k4);
                    }
                    else
                    {
                        k4 = 1000000000;
                        //                Console.WriteLine(k4);
                    }

                    k = Math.Min(Math.Min(k1, k2), Math.Min(k3, k4));

                    if (k < 1000000000)
                    {
                        Console.WriteLine(k);
                    }
                    else
                    {
                        Console.WriteLine("Poor Inna and pony!");
                    }
                }
                else
                {
                    Console.WriteLine("Poor Inna and pony!");
                }
            }

 //           Console.ReadKey();
        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace ConsoleApplication1
{
    class Program
    {

        public static void Main(string[] args)
        {

            string[] s = Console.ReadLine().Split(' ');
            int n, m, x, y, a, b;
            n = int.Parse(s[0]);
            m = int.Parse(s[1]);
            x = int.Parse(s[2]);
            y = int.Parse(s[3]);
            a = int.Parse(s[4]);
            b = int.Parse(s[5]);
            int[,] dist = new int[4, 2];
            dist[0, 0] = x - 1;
            dist[0, 1] = y - 1;
            dist[1, 0] = n - x;
            dist[1, 1] = y - 1;
            dist[2, 0] = n - x;
            dist[2, 1] = m - y;
            dist[3, 0] = x - 1;
            dist[3, 1] = m - y;
            int[] ris = new int[4];
            for (int i = 0; i < 4; i++)
            {
                if (((dist[i, 0] % (2 * a) == 0 && dist[i, 1] % (2 * b) == 0) || (dist[i, 0] % (2 * a) == a && dist[i, 1] % (2 * b) == b)))
                {
                    ris[i] = Math.Max((dist[i, 0] / a), (dist[i, 1] / b));
                }
                else { ris[i] = int.MaxValue; };
            }
            int min = ris.Min();
            if (min == 0)
            {
                Console.WriteLine(0);
            }
            else if (min == int.MaxValue||a>=n||b>=m)
            {
                Console.WriteLine("Poor Inna and pony!");
            }
            else
            {
                Console.WriteLine(min);
            }

        }
    }
}


using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Linq;
using System.Net;
using System.Numerics;
using System.Collections;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
using System.Threading;
using System.Net.Http;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Program
    {
        public class ThreadWork
        {

            private static readonly StreamReader reader = new StreamReader(Console.OpenStandardInput(1024 * 10), Encoding.ASCII, false, 1024 * 10);
            private static readonly StreamWriter writer = new StreamWriter(Console.OpenStandardOutput(1024 * 10), Encoding.ASCII, 1024 * 10);

            static public void Main()
            {
                var T = int.Parse(reader.ReadLine());
                for(var t2 = 0;t2 < T;t2++)
                {
                    var line = reader.ReadLine().Split(' ').Select(int.Parse).ToList();
                    var n = line[0];
                    var m = line[1];
                    var k = line[2];
                    var unknown = 0;
                    for(var i=1;i<n;i+=2)
                    {
                        for(var j=1;j<m;j+=2)
                        {
                            unknown += 2;
                        }
                    }
                    var horizontal = (n % 2 == 1 ? m / 2 : 0);
                    k -= horizontal;

                    writer.WriteLine(k >= 0 && k <= unknown && k%2==0 ? "YES" : "NO");
                }
                writer.Flush();
            }
        }
    }

}


using System;

namespace CodeforcesTasks.Contest1551
{
    // https://codeforces.com/contest/1551/problem/D1
    public class TaskD
    {
        static int[] Read() => Array.ConvertAll(Console.ReadLine()!.Split(), int.Parse);

        static void Main(string[] args)
        {
            var count = int.Parse(Console.ReadLine()!);
            for (var i = 0; i < count; i++)
            {
                var p = Read();
                Console.WriteLine(Solve(p[0], p[1], p[2]) ? "YES" : "NO");
            }
        }

        public static bool Solve(int n, int m, int k)
        {
            if (n % 2 == 0 && m % 2 == 0)
            {
                return k % 2 == 0;
            }

            if (n % 2 == 0)
            {
                var maxHorizontal = n * (m / 2);
                return k <= maxHorizontal && k % 2 == 0;
            }

            var horizontalSize = m / 2;
            return k >= horizontalSize && Solve(n - 1, m, k - horizontalSize);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Threading;
using System.Runtime.CompilerServices;
using System.Text;
using System.Diagnostics;
using System.Numerics;
using static System.Console;
using static System.Convert;
using static System.Math;
using static Template;

class Solver
{
    public Scanner sc;
    public void Solve()
    {
        N = sc.Int;
        var A = new int[N][];
        var R = new int[N * N+1];R[N * N] = -1;
        for (int i = 0; i < N; i++)
        {
            A[i] = sc.ArrInt;
            for (int j = 0; j < N; j++)
            {
                A[i][j]--;
                R[A[i][j]] = Id(i, j);
            }
        }
        //dist:=iにいて，jまでクリアしていて，駒が0:knight,1:bishop,2:rookのとき
        var dist = Create(N * N, () => Create(N * N, () => Create(3, () => long.MaxValue / 2)));
        var pq = new PriorityQueue<(long c, int now, int cl, int pi)>((a, b) => a.c.CompareTo(b.c));
        dist[R[0]][0][0] = dist[R[0]][0][1] = dist[R[0]][0][2] = 0;
        pq.Push((0, R[0], 0, 0));
        pq.Push((0, R[0], 0, 1));
        pq.Push((0, R[0], 0, 2));
        while (pq.Any())
        {
            var (cst, now, cl, pi) = pq.Pop();
            int h = now / N, w = now % N;
            if (pi == 0)
            {
                for(int k = 0; k < knH.Length; k++)
                {
                    int nh = h + knH[k], nw = w + knW[k];
                    if (!Inside(nh, nw, N, N)) continue;
                    var p = cl;
                    if (Id(nh, nw) == R[cl + 1]) p++;
                    if (chmin(ref dist[Id(nh, nw)][p][pi], cst + W))
                    {
                        pq.Push((cst + W, Id(nh, nw), p, pi));
                    }
                }
            }
            else if (pi == 1)
            {
                for(int i = -N + 1; i <= N - 1; i++)
                {
                    {
                        int nh = i + h, nw = i + w;
                        if (!Inside(nh, nw, N, N)) goto SEC;
                        var p = cl;
                        if (Id(nh, nw) == R[cl + 1]) p++;
                        if (chmin(ref dist[Id(nh, nw)][p][pi], cst + W))
                        {
                            pq.Push((cst + W, Id(nh, nw), p, pi));
                        }
                    }
                SEC:;
                    {
                        int nh = i + h, nw = -i + w;
                        if (!Inside(nh, nw, N, N)) continue;
                        var p = cl;
                        if (Id(nh, nw) == R[cl + 1]) p++;
                        if (chmin(ref dist[Id(nh, nw)][p][pi], cst + W))
                        {
                            pq.Push((cst + W, Id(nh, nw), p, pi));
                        }
                    }
                }
            }
            else
            {
                for(int i = -N + 1; i <= N - 1; i++)
                {

                    {
                        int nh = h, nw = i + w;
                        if (!Inside(nh, nw, N, N)) goto SEC;
                        var p = cl;
                        if (Id(nh, nw) == R[cl + 1]) p++;
                        if (chmin(ref dist[Id(nh, nw)][p][pi], cst + W))
                        {
                            pq.Push((cst + W, Id(nh, nw), p, pi));
                        }
                    }
                SEC:;
                    {
                        int nh = i + h, nw = w;
                        if (!Inside(nh, nw, N, N)) continue;
                        var p = cl;
                        if (Id(nh, nw) == R[cl + 1]) p++;
                        if (chmin(ref dist[Id(nh, nw)][p][pi], cst + W))
                        {
                            pq.Push((cst + W, Id(nh, nw), p, pi));
                        }
                    }
                }
            }
            for(int k = 0; k < 3; k++)
            {
                if(chmin(ref dist[now][cl][k],cst+1+W))
                {
                    pq.Push((cst + 1+W, now, cl, k));
                }
            }
        }
        var res = dist[R[N * N - 1]][N * N - 1].Min();
        Console.WriteLine($"{res / W} {res % W}");
    }
    int N;
    const long W = (long)1e9;
    public int Id(int h, int w) => h * N + w;
    int[] knH = new[] { -2, -2, -1, -1, 1, 1, 2, 2 }, knW = new[] { 1, -1, 2, -2, 2, -2, 1, -1 };
    public static bool Inside(int h, int w, int H, int W)
        => 0 <= h && h < H && 0 <= w && w < W;
}

public class PriorityQueue<T>
{
    private List<T> data = new List<T>();
    private Comparison<T> cmp;
    public int Count { get { return data.Count; } }
    public T Top { get { return data[0]; } }
    public PriorityQueue() { cmp = cmp ?? Comparer<T>.Default.Compare; }

    public PriorityQueue(Comparison<T> comparison) { cmp = comparison; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private int Parent(int i)
        => (i - 1) >> 1;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private int Left(int i)
        => (i << 1) + 1;
    public T Push(T val)
    {
        int i = data.Count;
        data.Add(val);
        while (i > 0)
        {
            int p = Parent(i);
            if (cmp(data[p], val) <= 0)
                break;
            data[i] = data[p];
            i = p;
        }
        data[i] = val;
        return val;
    }
    public T Pop()
    {
        var ret = data[0];
        var p = 0;
        var x = data[data.Count - 1];
        while (Left(p) < data.Count - 1)
        {
            var l = Left(p);
            if (l < data.Count - 2 && cmp(data[l + 1], data[l]) < 0) l++;
            if (cmp(data[l], x) >= 0)
                break;
            data[p] = data[l];
            p = l;
        }
        data[p] = x;
        data.RemoveAt(data.Count - 1);
        return ret;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public bool Any() => data.Count > 0;
}
#region Template
public static class Template
{
    static void Main(string[] args)
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        var sol = new Solver() { sc = new Scanner() };
        //var th = new Thread(sol.Solve, 1 << 26);th.Start();th.Join();
        sol.Solve();
        Console.Out.Flush();
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) > 0) { a = b; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void swap<T>(this IList<T> A, int i, int j) { var t = A[i]; A[i] = A[j]; A[j] = t; }
    public static T[] Create<T>(int n, Func<T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(); return rt; }
    public static T[] Create<T>(int n, Func<int, T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(i); return rt; }
    public static void Out<T>(this IList<T> A, out T a) => a = A[0];
    public static void Out<T>(this IList<T> A, out T a, out T b) { a = A[0]; b = A[1]; }
    public static void Out<T>(this IList<T> A, out T a, out T b, out T c) { A.Out(out a, out b); c = A[2]; }
    public static void Out<T>(this IList<T> A, out T a, out T b, out T c, out T d) { A.Out(out a, out b, out c); d = A[3]; }
    public static string Concat<T>(this IEnumerable<T> A, string sp) => string.Join(sp, A);
    public static char ToChar(this int s, char begin = '0') => (char)(s + begin);
    public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> A) => A.OrderBy(v => Guid.NewGuid());
    public static int CompareTo<T>(this T[] A, T[] B, Comparison<T> cmp = null) { cmp = cmp ?? Comparer<T>.Default.Compare; for (var i = 0; i < Min(A.Length, B.Length); i++) { int c = cmp(A[i], B[i]); if (c > 0) return 1; else if (c < 0) return -1; } if (A.Length == B.Length) return 0; if (A.Length > B.Length) return 1; else return -1; }
    public static int ArgMax<T>(this IList<T> A, Comparison<T> cmp = null) { cmp = cmp ?? Comparer<T>.Default.Compare; T max = A[0]; int rt = 0; for (int i = 1; i < A.Count; i++) if (cmp(max, A[i]) < 0) { max = A[i]; rt = i; } return rt; }
    public static T PopBack<T>(this List<T> A) { var v = A[A.Count - 1]; A.RemoveAt(A.Count - 1); return v; }
    public static void Fail<T>(T s) { Console.WriteLine(s); Console.Out.Close(); Environment.Exit(0); }
}
public class Scanner
{
    public string Str => Console.ReadLine().Trim();
    public int Int => int.Parse(Str);
    public long Long => long.Parse(Str);
    public double Double => double.Parse(Str);
    public int[] ArrInt => Str.Split(' ').Select(int.Parse).ToArray();
    public long[] ArrLong => Str.Split(' ').Select(long.Parse).ToArray();
    public char[][] Grid(int n) => Create(n, () => Str.ToCharArray());
    public int[] ArrInt1D(int n) => Create(n, () => Int);
    public long[] ArrLong1D(int n) => Create(n, () => Long);
    public int[][] ArrInt2D(int n) => Create(n, () => ArrInt);
    public long[][] ArrLong2D(int n) => Create(n, () => ArrLong);
    private Queue<string> q = new Queue<string>();
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public T Next<T>() { if (q.Count == 0) foreach (var item in Str.Split(' ')) q.Enqueue(item); return (T)Convert.ChangeType(q.Dequeue(), typeof(T)); }
    public void Make<T1>(out T1 v1) => v1 = Next<T1>();
    public void Make<T1, T2>(out T1 v1, out T2 v2) { v1 = Next<T1>(); v2 = Next<T2>(); }
    public void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3) { Make(out v1, out v2); v3 = Next<T3>(); }
    public void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4) { Make(out v1, out v2, out v3); v4 = Next<T4>(); }
    public void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5) { Make(out v1, out v2, out v3, out v4); v5 = Next<T5>(); }
    public void Make<T1, T2, T3, T4, T5, T6>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6) { Make(out v1, out v2, out v3, out v4, out v5); v6 = Next<T6>(); }
    public void Make<T1, T2, T3, T4, T5, T6, T7>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6, out T7 v7) { Make(out v1, out v2, out v3, out v4, out v5, out v6); v7 = Next<T7>(); }
    public (T1, T2) Make<T1, T2>() { Make(out T1 v1, out T2 v2); return (v1, v2); }
    public (T1, T2, T3) Make<T1, T2, T3>() { Make(out T1 v1, out T2 v2, out T3 v3); return (v1, v2, v3); }
    public (T1, T2, T3, T4) Make<T1, T2, T3, T4>() { Make(out T1 v1, out T2 v2, out T3 v3, out T4 v4); return (v1, v2, v3, v4); }
}

#endregion


using System;
using System.Collections.Generic;
using CompLib.Util;

public class Program
{
    private int N;
    private int[,] A;

    private int[] X, Y;

    private const int K = 3;

    private int[] kDx = new int[] {-2, -2, -1, -1, 1, 1, 2, 2};
    private int[] kDy = new int[] {-1, 1, -2, 2, -2, 2, -1, 1};

    private int[] rDx = new int[] {-1, 0, 0, 1};
    private int[] rDy = new int[] {0, 1, -1, 0};

    private int[] bDx = new int[] {1, 1, -1, -1};
    private int[] bDy = new int[] {-1, 1, -1, 1};

    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        A = new int[N, N];
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                A[i, j] = sc.NextInt() - 1;
            }
        }

        X = new int[N * N];
        Y = new int[N * N];
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                X[A[i, j]] = i;
                Y[A[i, j]] = j;
            }
        }

        var step = new int[N, N, N * N + 1, K];
        var r = new int[N, N, N * N + 1, K];
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                for (int k = 0; k <= N * N; k++)
                {
                    for (int l = 0; l < K; l++)
                    {
                        step[i, j, k, l] = int.MaxValue;
                    }
                }
            }
        }

        var q = new Queue<(int x, int y, int t, int k)>();
        for (int i = 0; i < K; i++)
        {
            step[X[0], Y[0], 1, i] = 0;
            q.Enqueue((X[0], Y[0], 1, i));
        }

        while (q.Count > 0)
        {
            var d = q.Dequeue();
            int curS = step[d.x, d.y, d.t, d.k];
            int curR = r[d.x, d.y, d.t, d.k];
            // 交換
            for (int i = 0; i < K; i++)
            {
                if (d.k == i) continue;
                ref int nextS = ref step[d.x, d.y, d.t, i];
                ref int nextR = ref r[d.x, d.y, d.t, i];
                if (curS + 1 < nextS || (curS + 1 == nextS && curR + 1 < nextR))
                {
                    nextS = curS + 1;
                    nextR = curR + 1;
                    q.Enqueue((d.x, d.y, d.t, i));
                }
            }

            // 移動
            if (d.k == 0)
            {
                // ナイト
                for (int i = 0; i < kDx.Length; i++)
                {
                    int nx = d.x + kDx[i];
                    int ny = d.y + kDy[i];
                    if (nx < 0 || nx >= N) continue;
                    if (ny < 0 || ny >= N) continue;
                    int nt = A[nx, ny] == d.t ? d.t + 1 : d.t;

                    ref int nextS = ref step[nx, ny, nt, 0];
                    ref int nextR = ref r[nx, ny, nt, 0];
                    if (curS + 1 < nextS || (curS + 1 == nextS && curR < nextR))
                    {
                        nextS = curS + 1;
                        nextR = curR;
                        q.Enqueue((nx, ny, nt, 0));
                    }
                }
            }
            else if (d.k == 1)
            {
                // ビショップ ななめ
                for (int i = 0; i < bDx.Length; i++)
                {
                    for (int j = 1;; j++)
                    {
                        int nx = d.x + bDx[i] * j;
                        int ny = d.y + bDy[i] * j;
                        if (nx < 0 || nx >= N) break;
                        if (ny < 0 || ny >= N) break;

                        int nt = A[nx, ny] == d.t ? d.t + 1 : d.t;

                        ref int nextS = ref step[nx, ny, nt, 1];
                        ref int nextR = ref r[nx, ny, nt, 1];
                        if (curS + 1 < nextS || (curS + 1 == nextS && curR < nextR))
                        {
                            nextS = curS + 1;
                            nextR = curR;
                            q.Enqueue((nx, ny, nt, 1));
                        }
                    }
                }
            }
            else if (d.k == 2)
            {
                // ルーク　たてよこ
                for (int i = 0; i < rDx.Length; i++)
                {
                    for (int j = 1;; j++)
                    {
                        int nx = d.x + rDx[i] * j;
                        int ny = d.y + rDy[i] * j;
                        if (nx < 0 || nx >= N) break;
                        if (ny < 0 || ny >= N) break;

                        int nt = A[nx, ny] == d.t ? d.t + 1 : d.t;

                        ref int nextS = ref step[nx, ny, nt, 2];
                        ref int nextR = ref r[nx, ny, nt, 2];
                        if (curS + 1 < nextS || (curS + 1 == nextS && curR < nextR))
                        {
                            nextS = curS + 1;
                            nextR = curR;
                            q.Enqueue((nx, ny, nt, 2));
                        }
                    }
                }
            }
        }

        // ゴール
        int ansS = int.MaxValue;
        int ansR = int.MaxValue;
        int lx = X[N * N - 1];
        int ly = Y[N * N - 1];
        for (int i = 0; i < 3; i++)
        {
            int ss = step[lx, ly, N * N, i];
            int rr = r[lx, ly, N * N, i];

            if (ss < ansS || (ss == ansS && rr < ansR))
            {
                ansS = ss;
                ansR = rr;
            }
        }

        Console.WriteLine($"{ansS} {ansR}");
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            if (_index >= _line.Length)
            {
                string s;
                do
                {
                    s = Console.ReadLine();
                } while (s.Length == 0);

                _line = s.Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public string ReadLine()
        {
            _index = _line.Length;
            return Console.ReadLine();
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            string s = Console.ReadLine();
            _line = s.Length == 0 ? new string[0] : s.Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}


/*
csc -debug D.cs && mono --debug D.exe <<< "3
1 9 3
8 6 7
4 2 5"
K: (1,1) -> (3,2) -> (1,3) -> (3,2) -> L -> (3,1) -> (3,3) -> (3,2) -> (2,2) -> (2,3) -> (2,1) -> (1,1) -> (1,2)
    1        2        3                      4        5                 6        7        8                 9
*/

using System;
using System.Collections.Generic;
using System.Linq;

public class HelloWorld {
    static public void Main () {
        SolveCodeForces();
    }

    private enum Fig { K, L, S };

    private static int N;
    private static int[][] arr;

    public static void SolveCodeForces() {
        N = int.Parse(Console.ReadLine());
        arr = new int[N][];
        for (var i = 0; i < arr.Length; i++)
            arr[i] = Console.ReadLine().Split().Select(int.Parse).ToArray();

        var numPos = new int[N*N+1][];
        for (var r = 0; r < arr.Length; r++)
            for (var c = 0; c < arr[r].Length; c++)
                numPos[ arr[r][c] ] = new[] {r, c};

        var bestAnswers = new Dictionary<Fig, int[]>{
            { Fig.K, new[] {0, 0} },
            { Fig.L, new[] {0, 0} },
            { Fig.S, new[] {0, 0} },
        };
        for (var d = 2; d <= N * N; d++) {
            var rcFrom = numPos[d - 1];
            var hopAns = new Dictionary<Fig, int[]>{
                { Fig.K, new[] {int.MaxValue, int.MaxValue} },
                { Fig.L, new[] {int.MaxValue, int.MaxValue} },
                { Fig.S, new[] {int.MaxValue, int.MaxValue} },
            };
            foreach (var fig in AllFigs) {
                if (bestAnswers[fig][0] < int.MaxValue)
                    foreach (var state in SolveHop(fig, rcFrom[0], rcFrom[1], d)) {
                        // System.Console.WriteLine("Digits: {0}->{1}, start fig {5}, end fig {2}, moves {3}, switches {4}", d-1, d, state.Fig, state.Moves, state.Switches, fig);
                        var newMoves = bestAnswers[fig][0] + state.Moves;
                        var newSwitches = bestAnswers[fig][1] + state.Switches;

                        var prevAns = hopAns[state.Fig];
                        if (prevAns[0] == int.MaxValue) {
                            prevAns[0] = newMoves;
                            prevAns[1] = newSwitches;
                        }
                        else if (newMoves < prevAns[0] || (newMoves == prevAns[0] && newSwitches < prevAns[1])) {
                            prevAns[0] = newMoves;
                            prevAns[1] = newSwitches;
                        }
                    }
            }
            bestAnswers = hopAns;
        }

        var bestAns = bestAnswers.OrderBy(kvp => kvp.Value[0]).ThenBy(kvp => kvp.Value[1]).First().Value;
        System.Console.WriteLine("{0} {1}", bestAns[0], bestAns[1]);
    }

    private struct State { public int R; public int C; public Fig Fig; public int Moves; public int Switches; }

    private static bool AreIndicesAllowed(int r, int c) {
        return r >= 0 && r < N && c >= 0 && c < N;
    }

    private static readonly int[][] K_Moves = new[] {
        new[] { -2, -1 },
        new[] { -2, 1 },
        new[] { -1, -2 },
        new[] { -1, 2 },
        new[] { 1, -2 },
        new[] { 1, 2 },
        new[] { 2, -1 },
        new[] { 2, 1 },
    };

    private static readonly int[][] S_Moves = new[] {
        new[] { -1, -1 },
        new[] { -1, 1 },
        new[] { 1, -1 },
        new[] { 1, 1 },
    };

    private static Fig[] AllFigs = new[] { Fig.L, Fig.S, Fig.K };

    private static IEnumerable<State> ExpandState(State state) {
        if (state.Fig == Fig.K) {
            foreach (var drdc in K_Moves) {
                var r = drdc[0] + state.R;
                var c = drdc[1] + state.C;
                if (AreIndicesAllowed(r, c))
                    yield return new State { Fig = Fig.K, R = r, C = c, Moves = state.Moves + 1, Switches = state.Switches };
            }
        }
        else if (state.Fig == Fig.L) {
            for (var r = 0; r < N; r++)
                if (r != state.R)
                    yield return new State { Fig = Fig.L, R = r, C = state.C, Moves = state.Moves + 1, Switches = state.Switches };
            for (var c = 0; c < N; c++)
                if (c != state.C)
                    yield return new State { Fig = Fig.L, R = state.R, C = c, Moves = state.Moves + 1, Switches = state.Switches };
        }
        else if (state.Fig == Fig.S) {
            for (var d = 1; d < N; d++)
                foreach (var drdc in S_Moves) {
                    var r = d * drdc[0] + state.R;
                    var c = d * drdc[1] + state.C;
                    if (AreIndicesAllowed(r, c))
                        yield return new State { Fig = Fig.S, R = r, C = c, Moves = state.Moves + 1, Switches = state.Switches };
                }
        }
        foreach (var fig in AllFigs)
            if (fig != state.Fig)
                yield return new State { Fig = fig, R = state.R, C = state.C, Moves = state.Moves + 1, Switches = state.Switches + 1 };
    }

    // Returns: end fig => [ number of moves, number of fig switches ]
    private static IEnumerable<State> SolveHop(Fig startFig, int rFrom, int cFrom, int targetDigit) {
        var Q = new Queue<State>();
        Q.Enqueue(new State { Fig = startFig, R = rFrom, C = cFrom });
        while (Q.Count > 0) {
            var state = Q.Dequeue();
            if (arr[state.R][state.C] == targetDigit)
                yield return state;
            // There is an always solution with 3 moves: switch to L, hop horizontally, hop vertically
            else if (state.Moves < 3)
                foreach (var neigh in ExpandState(state)) {
                    Q.Enqueue(neigh);
                }
        }
    }
}

public static class Helpers {
    public static T[] CreateArray<T>(int length, Func<int, T> itemCreate) {
        var result = new T[length];
        for (var i = 0; i < result.Length; i++)
            result[i] = itemCreate(i);
        return result;
    }
}


using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleTidbits
{
    class JustCodeForces
    {
        int N;
        int[,] board;
        Dictionary<int, Tuple<int, int>> locations;

        public static void Main()
        {
            JustCodeForces obj = new JustCodeForces();

            //while (true)
                obj.ThreePieces_ContestAnalysis();
        }

        int M;
        Tuple<int, int>[][] dist;
        Tuple<int, int>[] pos;

        int[] X = new int[] { 2, 2, -2, -2, 1, 1, -1, -1 };
        int[] Y = new int[] { -1, 1, -1, 1, 2, -2, 2, -2 };
        private bool In(int x, int y)
        {
            if (x >= 0 && y >= 0 && x < N && y < N) return true;
            return false;
        }

        private int Get(int x, int y, int p)
        {
            return x * 3 * N + y * 3 + p;
        }
        private void ThreePieces_ContestAnalysis()
        {
            N = int.Parse(Console.ReadLine());

            pos = new Tuple<int, int>[N * N];
            M = (N - 1) * N * 3 + N * 3 + 3;
            dist = new Tuple<int, int>[M][];
            for (int i = 0; i < M; i++)
                dist[i] = new Tuple<int, int>[M];

            for (int i = 0; i < N; i++)
            {
                var array = Array.ConvertAll(Console.ReadLine().Split(new char[] { ' ' }), int.Parse);

                for (int j = 0; j < N; j++)
                {
                    pos[--array[j]] = Tuple.Create(i, j);
                }
            }

            for (int i = 0; i < M; i++)
            {
                for (int j = 0; j < M; j++)
                {
                    dist[i][j] = Tuple.Create(100000, 100000);
                    if (i == j) dist[i][j] = Tuple.Create(0, 0);
                }
            }

            for (int i = 0; i < N; i++)
            {
                for (int j = 0; j < N; j++)
                {
                    //Get Knights distance
                    for (int k = 0; k < 8; k++)
                    {
                        int nx = i + X[k];
                        int ny = j + Y[k];

                        if (In(nx, ny))
                            dist[Get(i, j, 0)][Get(nx, ny, 0)] = Tuple.Create(1, 0);

                    }

                    //Get Bishops distances
                    for (int k = -N + 1; k <= N - 1; k++)
                    {
                        int nx = i + k;
                        int ny = j + k;

                        if (In(nx, ny))
                        {
                            dist[Get(i, j, 1)][Get(nx, ny, 1)] = Tuple.Create(1, 0);
                        }

                        ny = j - k;
                        if (In(nx, ny))
                            dist[Get(i, j, 1)][Get(nx, ny, 1)] = Tuple.Create(1, 0);
                    }

                    //Get Rook distance
                    for (int k = 0; k < N; k++)
                    {
                        int nx = i;
                        int ny = k;
                        dist[Get(i, j, 2)][Get(nx, ny, 2)] = Tuple.Create(1, 0);

                        nx = k;
                        ny = j;

                        dist[Get(i, j, 2)][Get(nx, ny, 2)] = Tuple.Create(1, 0);
                    }

                    //Replace at the exact location
                    for (int k = 0; k < 3; k++)
                    {
                        for (int m = 0; m < 3; m++)
                        {
                            if (k != m)
                                dist[Get(i, j, k)][Get(i, j, m)] = Tuple.Create(1, 1);
                        }
                    }
                }
            }

            for (int k = 0; k < M; k++)
            {
                for (int i = 0; i < M; i++)
                {
                    for (int j = 0; j < M; j++)
                    {
                        if (dist[i][j].Item1 > dist[i][k].Item1 + dist[k][j].Item1)
                        {
                            dist[i][j] = Tuple.Create(dist[i][k].Item1 + dist[k][j].Item1,
                                dist[i][k].Item2 + dist[k][j].Item2);
                        }
                        else if (dist[i][j].Item1 == dist[i][k].Item1 + dist[k][j].Item1
                            && dist[i][j].Item2 > dist[i][k].Item2 + dist[k][j].Item2)
                        {
                            dist[i][j] = Tuple.Create(dist[i][j].Item1, dist[i][k].Item2 + dist[k][j].Item2);
                        }
                    }
                }
            }

            var dp = new Tuple<int, int>[N * N][];
            for (int i = 0; i < N * N; i++)
            {
                dp[i] = new Tuple<int, int>[3];

                for (int k = 0; k < 3; k++)
                {
                    dp[i][k] = Tuple.Create(100000, 100000);
                }
            }
            dp[0][0] = Tuple.Create(0, 0);
            dp[0][1] = Tuple.Create(0, 0);
            dp[0][2] = Tuple.Create(0, 0);

            for (int i = 0; i < N * N - 1; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    for (int k = 0; k < 3; k++)
                    {
                        var new_min = dp[i][j].Item1 + dist[Get(pos[i].Item1, pos[i].Item2, j)][Get(pos[i + 1].Item1, pos[i + 1].Item2, k)].Item1;
                        if (dp[i + 1][k].Item1 > new_min)
                        {
                            dp[i + 1][k] = Tuple.Create(new_min, dp[i][j].Item2 + dist[Get(pos[i].Item1, pos[i].Item2, j)][Get(pos[i + 1].Item1, pos[i + 1].Item2, k)].Item2);
                        }
                        else if (dp[i + 1][k].Item1 == new_min
                            && dp[i + 1][k].Item2 > dp[i][j].Item2 + dist[Get(pos[i].Item1, pos[i].Item2, j)][Get(pos[i + 1].Item1, pos[i + 1].Item2, k)].Item2)
                        {
                            dp[i + 1][k] = Tuple.Create(new_min, dp[i][j].Item2 + dist[Get(pos[i].Item1, pos[i].Item2, j)][Get(pos[i + 1].Item1, pos[i + 1].Item2, k)].Item2);
                        }
                    }
                }
            }

            var res = dp[N * N - 1][0];
            if (res.Item1 > dp[N * N - 1][1].Item1
                || (res.Item1 == dp[N * N - 1][1].Item1 && res.Item2 > dp[N * N - 1][1].Item2))
            {
                res = dp[N * N - 1][1];
            }
            if (res.Item1 > dp[N * N - 1][2].Item1
                || (res.Item1 == dp[N * N - 1][2].Item1 && res.Item2 > dp[N * N - 1][2].Item2))
            {
                res = dp[N * N - 1][2];
            }

            Console.WriteLine("" + res.Item1 + " " + res.Item2);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Codeforces
{
#if _ONLINE_JUDGE_
	[OnlineJudge.Task("Codeforces 200C")]
#endif
	class Task200C
	{
		private InputTokenizer input = new InputTokenizer();
		private TextWriter output = Console.Out;

		struct Annoyed
		{
			public string name;
			public Team info;
		}

		struct Team
		{
			public int goals, missed_goals, plays, points;
		}

		struct Comp : IComparer<Annoyed>
		{
			public int Compare(Annoyed x, Annoyed y)
			{
				{
					int a = x.info.points, b = y.info.points;
					if (a != b) return b - a;
				}

				{
					int a = x.info.goals - x.info.missed_goals, b = y.info.goals - y.info.missed_goals;
					if (a != b) return b - a;
				}

				{
					int a = x.info.goals, b = y.info.goals;
					if (a != b) return b - a;
				}

				return x.name.CompareTo(y.name);
			}
		}

		private void Solve()
		{
			var teams = new Dictionary<String, Team>();

			string a, b, g;
			for (int i = 0; i < 5; ++i) {
				input.Line().Read(out a).Read(out b).Read(out g);
				int g1 = g[0] - '0', g2 = g[2] - '0';

				Team A = new Team(), B = new Team();
				if (teams.ContainsKey(a)) A = teams[a];
				if (teams.ContainsKey(b)) B = teams[b];

				if (g1 > g2) {
					A.points += 3;
				} else if (g1 < g2) {
					B.points += 3;
				} else {
					++A.points;
					++B.points;
				}

				A.goals += g1;
				A.missed_goals += g2;
				++A.plays;
				teams[a] = A;

				B.goals += g2;
				B.missed_goals += g1;
				++B.plays;
				teams[b] = B;
			}

			var comp = new Comp();

			var nt = (from x in teams
								select new Annoyed {
									name = x.Key,
									info = x.Value
								}).ToArray();

			int asd = -1, asr = -1;
			for (int i = 0; i < 4; ++i) {
				if (nt[i].name == "BERLAND")
					asd = i;
				else if (nt[i].info.plays == 2)
					asr = i;
			}

			int X = 10000, Y = 10000, dXY = 10000000;
			for (int _X = 0; _X < 100; ++_X)
				for (int _Y = 0; _Y < 100; ++_Y) {
					nt[asd].info.points += 3;
					nt[asd].info.goals += _X;
					nt[asd].info.missed_goals += _Y;

					nt[asr].info.goals += _Y;
					nt[asr].info.missed_goals += _X;

					var st = nt.OrderBy(t => t, comp).ToArray();
					if (_X > _Y && (st[0].name == "BERLAND" || st[1].name == "BERLAND") && (_X - _Y < dXY || (_X - _Y == dXY && _Y < Y))) {
						X = _X;
						Y = _Y;
						dXY = X - Y;
					}

					nt[asd].info.points -= 3;
					nt[asd].info.goals -= _X;
					nt[asd].info.missed_goals -= _Y;

					nt[asr].info.goals -= _Y;
					nt[asr].info.missed_goals -= _X;
				}

			if (X == 10000)
				output.WriteLine("IMPOSSIBLE");
			else
				output.WriteLine("{0}:{1}", X, Y);
		}

		#region
		public static void Main()
		{
			var obj = new Task200C();
			obj.Solve();
		}

		private class InputTokenizer
		{
			private List<string> _tokens = new List<string>();
			private int _offset = 0;

			public InputTokenizer()
			{
			}

			public InputTokenizer String(String s)
			{
				_tokens.AddRange(s.Split(new char[] { ' ', '\n', '\t', '\r' }, StringSplitOptions.RemoveEmptyEntries));
				return this;
			}

			public InputTokenizer Line()
			{
				return String(Console.In.ReadLine());
			}

			public InputTokenizer Read(out int v)
			{
				v = int.Parse(_tokens[_offset++]);
				return this;
			}

			public InputTokenizer Read(int n, out int[] v)
			{
				v = new int[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}

			public InputTokenizer Read(out long v)
			{
				v = long.Parse(_tokens[_offset++]);
				return this;
			}

			public InputTokenizer Read(int n, out long[] v)
			{
				v = new long[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}

			public InputTokenizer Read(out ulong v)
			{
				v = ulong.Parse(_tokens[_offset++]);
				return this;
			}

			public InputTokenizer Read(int n, out ulong[] v)
			{
				v = new ulong[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}

			public InputTokenizer Read(out double v)
			{
				v = double.Parse(_tokens[_offset++].Replace('.', ','));
				return this;
			}

			public InputTokenizer Read(int n, out double[] v)
			{
				v = new double[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}

			public InputTokenizer Read(out string v)
			{
				v = _tokens[_offset++];
				return this;
			}

			public InputTokenizer Read(int n, out string[] v)
			{
				v = new string[n];
				for (int i = 0; i < n; ++i) Read(out v[i]);
				return this;
			}
		}
		#endregion
	}
}


#define Online

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Diagnostics;
using System.Threading;

namespace CF
{
    delegate double F(double x);
    delegate decimal Fdec(decimal x);

    class Team : IComparable<Team>
    {
        public string name;
        public int goal;
        public int miss;
        public int point;
        public int cnt;
        public int CompareTo(Team y)
        {
            if (this.point > y.point)
            {
                return 1;
            }
            else
            {
                if (this.point == y.point)
                {
                    if (this.goal - this.miss > y.goal - y.miss)
                    {
                        return 1;
                    }
                    else
                    {
                        if (this.goal - this.miss == y.goal - y.miss)
                        {
                            if (this.goal > y.goal)
                            {
                                return 1;
                            }
                            else
                            {
                                if (this.goal == y.goal)
                                {
                                    if (this.name.CompareTo(y.name) < 0)
                                        return 1;
                                    else
                                        return -1;
                                }
                                else
                                {
                                    return -1;
                                }
                            }

                        }
                        else
                        {
                            return -1;
                        }
                    }
                }
                else
                {
                    return -1;
                }
            }
        }
    }

    class Program
    {

        static void Main(string[] args)
        {

#if FileIO
            StreamReader sr = new StreamReader("input.txt");
            StreamWriter sw = new StreamWriter("output.txt");
            Console.SetIn(sr);
            Console.SetOut(sw);
#endif

            Team[] t = new Team[4];
            for (int i = 0; i < 5; i++)
            {
                string[] input = ReadArray();
                string[] score = input[2].Split(':');
                int x = int.Parse(score[0]);
                int y = int.Parse(score[1]);
                for (int j = 0; j < 4; j++)
                {
                    if (t[j] == null)
                    {
                        t[j] = new Team();
                        t[j].name = input[0];
                    }
                    if (t[j].name.Equals(input[0]))
                    {
                        if (x > y) t[j].point += 3;
                        if (x == y) t[j].point++;
                        t[j].goal += x;
                        t[j].miss += y;
                        t[j].cnt++;
                        break;
                    }

                }
                for (int j = 0; j < 4; j++)
                {
                    if (t[j] == null)
                    {
                        t[j] = new Team();
                        t[j].name = input[1];
                    }
                    if (t[j].name.Equals(input[1]))
                    {
                        if (y > x) t[j].point += 3;
                        if (x == y) t[j].point++;
                        t[j].goal += y;
                        t[j].miss += x;
                        t[j].cnt++;
                        break;
                    }
                }
            }

            for (int i = 0; i < 4; i++)
                if (t[i].name.Equals("BERLAND"))
                    t[i].point += 3;

            Team[] L = new Team[4];
            Team[] R = new Team[4];
            ArrayUtils.Merge_Sort(t, 0, 3, L, R);

            int ber, opp;
            ber = opp = 0;
            for (int i = 0; i < 4; i++)
            {
                if (t[i].name.Equals("BERLAND"))
                {
                    ber = i;
                }
                else if (t[i].cnt == 2)
                {
                    opp = i;
                }
            }

            int dif_max = int.MinValue;
            int goal_max = int.MinValue;
            for (int i = 0; i < 4; i++)
            {
                if (i == ber) continue;
                dif_max = Math.Max(t[i].goal - t[i].miss + Math.Abs(t[ber].goal - t[ber].miss), dif_max);
                goal_max = Math.Max(t[i].goal, goal_max);
            }
            for (int i = 1; i <= dif_max; i++)
            {

                for (int j = i; j <= goal_max + 1; j++)
                {
                    int comp = 0;
                    t[opp].goal += j - i;
                    t[opp].miss += j;
                    t[ber].goal += j;
                    t[ber].miss += j - i;
                    for (int z = 0; z < 4; z++)
                    {
                        if (z == ber) continue;
                        if (t[ber].CompareTo(t[z]) > 0)
                            comp++;
                    }
                    if (comp >= 2)
                    {
                        Console.WriteLine(j + ":" + (j - i));
                        return;
                    }
                    else
                    {
                        t[opp].goal -= j - i;
                        t[opp].miss -= j;
                        t[ber].goal -= j;
                        t[ber].miss -= j - i;
                    }
                }
            }
            Console.WriteLine("IMPOSSIBLE");

#if Online
            Console.ReadLine();
#endif

#if FileIO
            sr.Close();
            sw.Close();
#endif
        }

        static string[] ReadArray()
        {
            return Console.ReadLine().Split(' ');
        }

        static int ReadInt()
        {
            return int.Parse(Console.ReadLine());
        }
    }

   /* public static class MyMath
    {
        public static long BinPow(long a, long n, long mod)
        {
            long res = 1;
            while (n > 0)
            {
                if ((n & 1) == 1)
                {
                    res = (res * a) % mod;
                }
                a = (a * a) % mod;
                n >>= 1;
            }
            return res;
        }

        public static long GCD(long a, long b)
        {
            while (b != 0) b = a % (a = b);
            return a;
        }

        public static int GCD(int a, int b)
        {
            while (b != 0) b = a % (a = b);
            return a;
        }

        public static long LCM(long a, long b)
        {
            return (a * b) / GCD(a, b);
        }

        public static int LCM(int a, int b)
        {
            return (a * b) / GCD(a, b);
        }
    }

    public class SegmentTree
    {
        int[] v_min;
        int[] v_max;
        int[] mas;
        public void BuildTree(int[] a)
        {
            int n = a.Length - 1;
            int z = 0;
            while (n >= 2)
            {
                z++;
                n >>= 1;
            }
            n = 1 << (z + 1);
            v_min = new int[n << 1];
            v_max = new int[n << 1];
            mas = new int[n << 1];
            for (int i = n; i < n + a.Length; i++)
                v_min[i] = a[i - n];
            for (int i = n + a.Length; i < v_min.Length; i++)
                v_min[i] = int.MaxValue;
            for (int i = n - 1; i > 0; i--)
                v_min[i] = Math.Min(v_min[(i << 1)], v_min[(i << 1) + 1]);

            for (int i = n; i < n + a.Length; i++)
                v_max[i] = a[i - n];
            for (int i = n + a.Length; i < v_max.Length; i++)
                v_max[i] = int.MinValue;
            for (int i = n - 1; i > 0; i--)
                v_max[i] = Math.Max(v_max[(i << 1)], v_max[(i << 1) + 1]);

        }
        //nil
        public int RMQ_MIN(int l, int r)
        {
            int ans = int.MaxValue;
            l += v_min.Length >> 1;
            r += v_min.Length >> 1;
            const int o = 1;
            while (l <= r)
            {
                if ((l & 1) == o)
                    ans = Math.Min(ans, v_min[l]);
                if (!((r & 1) == 0))
                    ans = Math.Min(ans, v_min[r]);
                l = (l + 1) >> 1;
                r = (r - 1) >> 1;
            }
            return ans;
        }

        public int RMQ_MAX(int l, int r)
        {
            int ans = int.MinValue;
            l += v_max.Length >> 1;
            r += v_max.Length >> 1;
            const int o = 1;
            while (l <= r)
            {
                if ((l & 1) == o)
                    ans = Math.Max(ans, v_max[l]);
                if (!((r & 1) == 0))
                    ans = Math.Max(ans, v_max[r]);
                l = (l + 1) >> 1;
                r = (r - 1) >> 1;
            }
            return ans;
        }

        public void Put(int l, int r, int op)
        {
            l += mas.Length >> 1;
            r += mas.Length >> 1;
            while (l <= r)
            {
                if (l % 2 != 0)
                    mas[l] = op;
                if (r % 2 == 0)
                    mas[r] = op;
                l = (l + 1) >> 1;
                r = (r - 1) >> 1;
            }
        }

        public int Get(int x)
        {
            x += mas.Length >> 1;
            int ans = int.MinValue;
            while (x > 0)
            {
                ans = Math.Max(mas[x], ans);
                x >>= 1;
            }
            return ans;
        }

        public void Update(int i, int x)
        {
            i += v_min.Length >> 1;
            v_min[i] = x;
            v_max[i] = x;
            i >>= 1;
            while (i > 0)
            {
                v_min[i] = Math.Min(v_min[(i << 1)], v_min[(i << 1) + 1]);
                v_max[i] = Math.Max(v_max[(i << 1)], v_max[(i << 1) + 1]);
                i >>= 1;
            }
        }
    }

    class Edge
    {
        public int a;
        public int b;
        public int w;

        public Edge(int a, int b, int w)
        {
            this.a = a;
            this.b = b;
            this.w = w;
        }
    }

   /* class Graph
    {
        public List<int>[] g;
        public int[] color; //0-white, 1-gray, 2-black
        public int[] o; //open
        public int[] c; //close
        public int[] p;
        public int[] d;
        public List<Edge> e;

        public Graph(int n)
        {
            g = new List<int>[n + 1];
            color = new int[n + 1];
            o = new int[n + 1];
            c = new int[n + 1];
            p = new int[n + 1];
            d = new int[n + 1];
        }
    }

    static class GraphUtils
    {
        const int white = 0;
        const int gray = 1;
        const int black = 2;

        public static void BFS(int s, Graph g)
        {
            Queue<int> q = new Queue<int>();
            q.Enqueue(s);
            while (q.Count != 0)
            {
                int u = q.Dequeue();
                for (int i = 0; i < g.g[u].Count; i++)
                {
                    int v = g.g[u][i];
                    if (g.color[v] == white)
                    {
                        g.color[v] = gray;
                        g.d[v] = g.d[u] + 1;
                        g.p[v] = u;
                        q.Enqueue(v);
                    }
                }
                g.color[u] = black;
            }
        }

        public static void DFS(int u, ref int time, Graph g)
        {
            g.color[u] = gray;
            time++;
            g.o[u] = time;
            for (int i = 0; i < g.g[u].Count; i++)
            {
                int v = g.g[u][i];
                if (g.color[v] == white)
                {
                    g.p[v] = u;
                    DFS(v, ref time, g);
                }
            }
            g.color[u] = black;
            g.c[u] = time;
            time++;
        }

        public static void FordBellman(int u, Graph g)
        {
            int n = g.g.Length;
            for (int i = 0; i <= n; i++)
            {
                g.d[i] = int.MaxValue;
                g.p[i] = 0;
            }

            g.d[u] = 0;

            for (int i = 0; i < n - 1; i++)
            {
                for (int j = 0; j < g.e.Count; j++)
                {
                    if (g.d[g.e[j].a] != int.MaxValue)
                    {
                        if (g.d[g.e[j].a] + g.e[j].w < g.d[g.e[j].b])
                        {
                            g.d[g.e[j].b] = g.d[g.e[j].a] + g.e[j].w;
                            g.p[g.e[j].b] = g.e[j].a;
                        }
                    }
                }
            }
        }
    }*/
    static class ArrayUtils
    {
        public static int BinarySearch(int[] a, int val)
        {
            int left = 0;
            int right = a.Length - 1;
            int mid;

            while (left < right)
            {
                mid = left + ((right - left) >> 1);
                if (val <= a[mid])
                {
                    right = mid;
                }
                else
                {
                    left = mid + 1;
                }
            }

            if (a[left] == val)
                return left;
            else
                return -1;
        }

        public static double Bisection(F f, double left, double right, double eps)
        {
            double mid;
            while (right - left > eps)
            {
                mid = left + ((right - left) / 2d);
                if (Math.Sign(f(mid)) != Math.Sign(f(left)))
                    right = mid;
                else
                    left = mid;
            }
            return (left + right) / 2d;
        }

        public static decimal TernarySearchDec(Fdec f, decimal eps, decimal l, decimal r, bool isMax)
        {
            decimal m1, m2;
            while (r - l > eps)
            {
                m1 = l + (r - l) / 3m;
                m2 = r - (r - l) / 3m;
                if (f(m1) < f(m2))
                    if (isMax)
                        l = m1;
                    else
                        r = m2;
                else
                    if (isMax)
                        r = m2;
                    else
                        l = m1;
            }
            return r;
        }

        public static double TernarySearch(F f, double eps, double l, double r, bool isMax)
        {
            double m1, m2;
            while (r - l > eps)
            {
                m1 = l + (r - l) / 3d;
                m2 = r - (r - l) / 3d;
                if (f(m1) < f(m2))
                    if (isMax)
                        l = m1;
                    else
                        r = m2;
                else
                    if (isMax)
                        r = m2;
                    else
                        l = m1;
            }
            return r;
        }

        public static void Merge<T>(T[] A, int p, int q, int r, T[] L, T[] R) where T : IComparable<T>
        {
            for (int i = p; i <= q; i++)
                L[i] = A[i];
            for (int i = q + 1; i <= r; i++)
                R[i] = A[i];

            for (int k = p, i = p, j = q + 1; k <= r; k++)
            {
                if (i > q)
                {
                    for (; k <= r; k++, j++)
                        A[k] = R[j];
                    return;
                }
                if (j > r)
                {
                    for (; k <= r; k++, i++)
                        A[k] = L[i];
                    return;
                }
                if (L[i].CompareTo(R[j]) <= 0)
                {
                    A[k] = L[i];
                    i++;
                }
                else
                {
                    A[k] = R[j];
                    j++;
                }
            }
        }

        public static void Merge_Sort<T>(T[] A, int p, int r, T[] L, T[] R) where T : IComparable<T>
        {
            if (p >= r) return;
            int q = p + ((r - p) >> 1);
            Merge_Sort(A, p, q, L, R);
            Merge_Sort(A, q + 1, r, L, R);
            Merge(A, p, q, r, L, R);
        }

        static void Shake<T>(T[] a)
        {
            Random rnd = new Random(Environment.TickCount);
            T temp;
            int b, c;
            for (int i = 0; i < a.Length; i++)
            {
                b = rnd.Next(0, a.Length);
                c = rnd.Next(0, a.Length);
                temp = a[b];
                a[b] = a[c];
                a[c] = temp;
            }
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Contest_63
{
    class Program
    {
        class mem : IComparable
        {
            public int i;
            public int p;
            //public string name;

            public mem(int i, int p)
            {
                this.i = i; this.p = p;
            }

            public int CompareTo(Object k)
            {
                var a = (mem)k;
                if (this.p != a.p)
                    return this.p.CompareTo(a.p);
                return this.i.CompareTo(a.i);
            }
        }

        static void A()
        {
            int n = int.Parse(Console.ReadLine());

            SortedList<mem, string> m = new SortedList<mem, string>();

            for (int i = 0; i < n; i++)
            {
                var t = Console.ReadLine().Split(' ');
                int p = 0;
                switch (t[1])
                {
                    case "rat":
                        p = 1;
                        break;
                    case "child":
                        case "woman":
                        p = 2;
                        break;
                    case "man":
                        p = 3;
                        break;
                    case "captain":
                        p = 4;
                        break;
                }

                m.Add(new mem(i, p), t[0]);
            }

            foreach (var e in m)
                Console.WriteLine(e.Value);
        }

        static void B()
        {
            var t = Console.ReadLine().Split(' ');
            int n = int.Parse(t[0]);
            int m = int.Parse(t[1]);

            SortedDictionary<int, int> d = new SortedDictionary<int, int>();
            for (int i = 1; i <= m; i++)
                d.Add(i,0);

            t = Console.ReadLine().Split(' ');

            foreach (var s in t)
            {
                int k = int.Parse(s);
                d[k]++;
            }

            int l = 0;
            for (; d[m] != n; l++)
            {
                for (int j = m-1; j > 0; j--)
                {
                    if (d[j] > 0)
                    {
                        d[j + 1]++;
                        d[j]--;
                    }
                }
            }
            Console.WriteLine(l);
        }

        static bool check(string s1, string s2, int x, int y)
        {
            int kx = 0;
            int ky = 0;

            for (int i = 0; i < 4; i++)
                if (s1[i] == s2[i])
                    kx++;
            if (kx != x)
                return false;
            ky = s1.Intersect(s2).Count();
            if (ky - kx != y)
                return false;
            return true;
        }

        static void C()
        {
            var l = new List<string>();
            int[] a = new int[4];
            for (a[0] = 0; a[0] <= 9; a[0]++)
                for (a[1] = 0; a[1] <= 9; a[1]++)
                    for (a[2] = 0; a[2] <= 9; a[2]++)
                        for (a[3] = 0; a[3] <= 9; a[3]++)
                        {
                            string s = "" + a[0] + a[1] + a[2] + a[3];
                            if (!((a[0] == a[1]) || (a[0] == a[2]) || (a[0] == a[3]) || (a[1] == a[2]) || (a[1] == a[3]) || (a[2] == a[3])))
                                l.Add(s);
                        }

            int n = int.Parse(Console.ReadLine());

            for (int i = 0; i < n; i++)
            {
                var t = Console.ReadLine().Split(' ');
                string b = t[0];
                int x = int.Parse(t[1]);
                int y = int.Parse(t[2]);
                for (int k = 0;k < l.Count; k++)
                {
                    if (!check(l[k], b, x, y))
                    {
                        l.Remove(l[k]);
                        k--;
                    }
                }
            }

            if (l.Count > 1)
                Console.WriteLine("Need more data");
            if (l.Count == 0)
                Console.WriteLine("Incorrect data");
            if (l.Count == 1)
                Console.WriteLine(l[0]);
        }

        static void Main(string[] args)
        {
            C();
        }
    }
}