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

class Solver
{
    public void Solve(Scanner sc)
    {
        var (N, s) = (sc.Int, sc.Str);
        WriteLine(N + 1);

    }

    public List<(T item, int len)> RLEncoding<T>(IList<T> list, Comparison<T> cmp = null)
    {
        cmp = cmp ?? Comparer<T>.Default.Compare;
        var rt = new List<(T, int)> { (list[0], 1) };
        for (var i = 1; i < list.Count; i++)
            if (cmp(rt[1].Item1, list[i]) == 0)
                rt[1] = (rt[1].Item1, rt[1].Item2 + 1);
            else rt.Add((list[i], 1));
        return rt;
    }
}

#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) == 1) { 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) == -1) { 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 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; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    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 int PopCount(int n) => (int)System.Runtime.Intrinsics.X86.Popcnt.PopCount((uint)n);
    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 T Next<T>() { if (q.Count == 0) foreach (var item in Str.Split(' ').AsSpan()[..]) 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 (T1, T2) Make<T1, T2>() { (T1, T2) rt; Make(out rt.Item1, out rt.Item2); return rt; }
    public (T1, T2, T3) Make<T1, T2, T3>() { (T1, T2, T3) rt; Make(out rt.Item1, out rt.Item2, out rt.Item3); return rt; }
    public (T1, T2, T3, T4) Make<T1, T2, T3, T4>() { (T1, T2, T3, T4) rt; Make(out rt.Item1, out rt.Item2, out rt.Item3, out rt.Item4); return rt; }
}

#endregion


            int sum = 0;
            int[] segmant = { 6, 2, 5, 5, 4, 5, 6, 3, 7, 6, 8, 4, 7, 7, 6, 7 };
            Console.WriteLine("Enter from number: ");
            int a = int.Parse(Console.ReadLine());

            Console.WriteLine("Enter to number: ");
            int b = int.Parse(Console.ReadLine());

            for (int i = a; i < segmant.Length; i++)
            {
                sum = sum + segmant[i];
                if (i == b)
                {
                    break;
                }
            }

            Console.WriteLine("The total is: " + sum);
            Console.ReadKey();


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

namespace AprilFools
{
    internal class Program
    {
        private const string TestIn = @"..\..\In\Text.in";
        private const string TestOut = @"..\..\In\Text.out";

        private static void Solve()
        {
            var input = ReadArray();
            var res = Convert.ToInt32(input[0]);

            var r2 = Convert.ToInt32(string.Join("", input[1].ToCharArray().Reverse()));

            Console.WriteLine(res+r2);
        }

        private static void Main()
        {
            if (Debugger.IsAttached)
            {
                Console.SetIn(new StreamReader(TestIn));
                Console.SetOut(new StreamWriter(TestOut));
            }

            Solve();

            if (Debugger.IsAttached)
            {
                Console.In.Close();
                Console.Out.Close();
                Console.SetIn(new StreamReader(Console.OpenStandardInput()));
                Console.ReadLine();
            }
        }

        #region Reader

        private static string Read()
        {
            return Console.ReadLine();
        }

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

        private static List<int> ReadIntArray()
        {
            var input = Console.ReadLine().Split(' ').Select(Int32.Parse).ToList();
            return input;
        }

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

        private static long ReadLong()
        {
            return long.Parse(Console.ReadLine());
        }

        private static int ReadNextInt(string[] input, int index)
        {
            return Int32.Parse(input[index]);
        }

        #endregion
    }
}


using System;

class Program
{
    static int Main()
    {
        int a, b, c;
        string []s = Console.ReadLine().Split(' ');
        a = Convert.ToInt32(s[0]);
        b = Convert.ToInt32(s[1]);
        c = Convert.ToInt32(s[2]);
        Console.WriteLine(a * b * c - (a - 1) * (b - 1) * (c - 1));
    }
}


static void Main(string[] args)
        {
            var i = Convert.ToInt64(Console.ReadLine());
            if (i == 3)
                i = 24;
            else
            {
                long q = 0;
                for (var k = 0; k < i - 1; ++k)
                {
                    if (k == 0 || k == i - 2)
                    {
                        q += (long)Math.Pow(4,i - 3) * 3;
                    }
                    else
                    {
                        q += (long)Math.Pow(4, (i - 4)) * 9;
                    }
                }

                i = q * 4;
            }
            Console.WriteLine(i);
        }


#define _LOCAL_VAN

using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Printing;
using System.IO;

namespace ConsoleApplication1
{

    class Program
    {
        public class TaskE
        {
            private long x, y;

            public void ReadInput()
            {
#if DEBUG
                Console.SetIn(File.OpenText("Test.txt"));
#endif
                var arr = Console.ReadLine().Split(" ".ToCharArray()).Select(x => long.Parse(x)).ToArray();
                x = arr[0];
                y = arr[1];
            }

            public BigInteger Mx(long a, long b)
            {
                return new BigInteger(a) * b;
            }

            public long Bs(Func<long, bool> F)
            {
                long l = 1;
                long r = (long) 1e18;
                while (l <= r)
                {
                    long mid = (l + r)/2;
                    bool res = F(mid);
                    if (!res)
                        l = mid + 1;
                    else
                        r = mid - 1;
                }
                return l;
            }

            public void Solve()
            {
                ReadInput();
                long xA = 1;
                long xB = 0;
                long yA = 0;
                long yB = 1;
                long xM = 1;
                long yM = 1;

                List<Tuple<long, bool>> ans = new List<Tuple<long, bool>>();
                while (Mx(xM, y) != Mx(yM, x))
                {
                    long xMn, yMn;
                    bool p;
                    long n;
                    if (Mx(x, yM) > Mx(y, xM)) // Alice: x/y > xM/yM
                    {
                        n = Bs(k => Mx(xM + k * xA, y) >= Mx(yM + k * yA, x)); // xM + k * xA / yM + k * yA >= x / y
                        p = true;
                        xB = xM + (n - 1) * xA;
                        yB = yM + (n - 1) * yA;
                        xM = xM + n * xA;
                        yM = yM + n * yA;
                    }
                    else // Bob: x/y < xM/yM
                    {
                        n = Bs(k => Mx(xM + k * xB, y) <= Mx(yM + k * yB, x)); // xM + k * xA / yM + k * yA <= x / y
                        p = false;
                        xA = xM + (n - 1) * xB;
                        yA = yM + (n - 1) * yB;
                        xM = xM + n * xB;
                        yM = yM + n * yB;
                    }
                    ans.Add(Tuple.Create(n, p));
                }
                if (xM != x || yM != y)
                {
                    Console.WriteLine("Impossible");
                    return;
                }

                foreach (var t in ans)
                {
                    Console.Write("{0}{1}", t.Item1, t.Item2 ? 'A' : 'B');
                }
            }

        }

        static void Main(string[] args)
        {
            TaskE e = new TaskE();
            e.Solve();
        }
    }
}

/*
5372

548311022282575478768582833326463951961872181
548311022282908812101916166659797285295205514
*/


 static void Main(string[] args)
        {
            string exp;
            exp=Console.ReadLine();
            int i,a=0,b=0,c=0;
            string A = "", B = "", C = "";
            for (i = 0; i < exp.Length; i++)
            {
                if(i < exp.IndexOf('+'))
                {
                    a++;
                    A += exp[i];
                }

                if(i > exp.IndexOf('+') && i<exp.IndexOf('='))
                {
                    b++;
                    B += exp[i];
                }
                if(exp.IndexOf('=') < i )
                {
                    c++;
                    C += exp[i];
                }

            }

            string Aminus1 = A.Remove(A.Length - 1, 1);
            string Bminus1 = B.Remove(B.Length - 1, 1);
            string Cminus1 = C.Remove(C.Length - 1, 1);

            if (a + b == c)
                Console.WriteLine(exp);
            else if (a - 1 + b == c + 1)
                Console.WriteLine("{0}+{1}={2}", Aminus1,B,C+'|');
            else if (a + b - 1 == c + 1)
                Console.WriteLine("{0}+{1}={2}", A, Bminus1, C + '|');
            else if (a + 1 + b == c - 1)
                Console.WriteLine("{0}+{1}={2}", A+'|', B, Cminus1);
            else if (a + b + 1 == c - 1)
                Console.WriteLine("{0}+{1}={2}", A, B + '|', Cminus1);
            else
                Console.WriteLine("Impossible");

        }


using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tidbits;

 class MinHeap<T> where T : IComparable<T>
    {
        public IList<T> m_array;

        public MinHeap()
        {
            m_array = new List<T>();
        }
        public T GetRoot()
        {
            return m_array[0];
        }

        public T ExtractMin()
        {
            var result = m_array[0];
            m_array[0] = m_array[Count - 1];
            m_array.RemoveAt(m_array.Count - 1);
            ShiftDown(0);

            return result;
        }

        public void Remove(int index)
        {
        }
        public void ShiftDown(int index)
        {
            int left = 2 * index + 1;
            int right = 2 * index + 2;
            int Index = index;

            if (left < Count && m_array[index].CompareTo(m_array[left]) > 0)
            {
                index = left;
            }

            if (right < Count && m_array[index].CompareTo(m_array[right]) > 0)
            {
                index = right;
            }

            if (index != Index)
            {
                var temp = m_array[Index];
                m_array[Index] = m_array[index];
                m_array[index] = temp;

                ShiftDown(index);
            }
        }

        public int Count
        {
            get
            {
                return m_array.Count();
            }
        }

        private void Balance(int index)
        {
            if (index < 0)
            {
                return;
            }

            int parent = (index - 1) / 2;

            if (m_array[parent].CompareTo(m_array[index]) > 0)
            {
                var temp = m_array[parent];
                m_array[parent] = m_array[index];
                m_array[index] = temp;
                Balance(parent);
            }
        }

        public void Add(T number)
        {
            m_array.Add(number);
            BuildHeap();
            //Balance(m_array.Count - 1);
        }

        private void BuildHeap()
        {
            for (int i = Count / 2; i >= 0; i--)
            {
                Heapify(m_array, i);
            }
        }

        private void Heapify(IList<T> array, int index)
        {
            int N = array.Count;

            int Index = index;
            int left = 2 * index + 1;
            int right = 2 * index + 2;

            if (left < N && array[left].CompareTo(array[index]) < 0)
            {
                index = left;
            }

            if (right < N && array[right].CompareTo(array[index]) < 0)
            {
                index = right;
            }

            if (index != Index)
            {
                var temp = array[Index];
                array[Index] = array[index];
                array[index] = temp;

                Heapify(array, index);
            }

        }
    }

class testcodeforces
{
    int N;
    int[,] board;
    Dictionary<int, Tuple<int, int>> locations;

    public static void Main()
    {
        testcodeforces obj = new testcodeforces();
        obj.ThreePieces();
    }

    enum Pawn
    {
        Bishop = 0,
        Knight,
        Rook
    }
    class Move : IComparable<Move>
    {
        public int Moves { get; set; }
        public int Rep { get; set; }

        public int Num { get; set; }
        public Pawn pawn { get; set; }
        public int CompareTo(Move other)
        {
            if (Moves < other.Moves) return -1;
            if (Moves > other.Moves) return 1;

            if (Moves == other.Moves) return Rep.CompareTo(other.Rep);

            return 0;
        }
    }
    private void ThreePieces()
    {
        N = int.Parse(Console.ReadLine());

        board = new int[N, N];
        locations = new Dictionary<int, Tuple<int, int>>();

        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++)
            {
                board[i, j] = array[j];
                locations.Add(array[j], Tuple.Create(i + 1, j + 1));
            }
        }

        //Min of all
        #region rook Distances
        //Find rook distances
        var rook_distances = new Tuple<int, int>[(N * N) + 1];
        rook_distances[1] = Tuple.Create(0, 0);

        for (int i = 2; i <= N * N; i++)
        {
            var current_location = locations[i - 1];
            var new_location = locations[i];

            if (current_location.Item1 == new_location.Item1 ||
                current_location.Item2 == new_location.Item2)
                rook_distances[i] = Tuple.Create(1, 0);
            else
                rook_distances[i] = Tuple.Create(2, 0);
        }
        #endregion

        #region Bishop Distances
        //Find Bishop Distances
        var bishop_distances = new Tuple<int, int>[(N * N) + 1];
        bishop_distances[1] = Tuple.Create(0, 0);

        for (int i = 2; i <= N * N; i++)
        {
            var current_location = locations[i - 1];
            var new_location = locations[i];

            if ((current_location.Item1 + current_location.Item2) % 2 != (new_location.Item1 + new_location.Item2) % 2)
                bishop_distances[i] = Tuple.Create(int.MaxValue, 0);
            else
            {
                bool found = false;
                //Move North East
                for (int k = 1; k <= Math.Min(current_location.Item1 - 1, N - current_location.Item2); k++)
                {
                    if (new_location.Item1 == current_location.Item1 - k && new_location.Item2 == current_location.Item2 + k)
                    {
                        bishop_distances[i] = Tuple.Create(1, 0);
                        found = true;
                        break;
                    }
                }

                //Move South west
                if (!found)
                {
                    for (int k = 1; k <= Math.Min(N - current_location.Item1, current_location.Item2 - 1); k++)
                    {
                        if (new_location.Item1 == current_location.Item1 + k && new_location.Item2 == current_location.Item2 - k)
                        {
                            bishop_distances[i] = Tuple.Create(1, 0);
                            found = true;
                            break;
                        }
                    }
                }

                //Move South east
                if (!found)
                {
                    for (int k = 1; k <= Math.Min(N - current_location.Item1, N - current_location.Item2 - 1); k++)
                    {
                        if (new_location.Item1 == current_location.Item1 + k && new_location.Item2 == current_location.Item2 + k)
                        {
                            bishop_distances[i] = Tuple.Create(1, 0);
                            found = true;
                            break;
                        }
                    }
                }

                //Move North West
                if (!found)
                {
                    for (int k = 1; k <= Math.Min(current_location.Item1 - 1, current_location.Item2 - 1); k++)
                    {
                        if (new_location.Item1 == current_location.Item1 - k && new_location.Item2 == current_location.Item2 - k)
                        {
                            bishop_distances[i] = Tuple.Create(1, 0);
                            found = true;
                            break;
                        }
                    }
                }

                if (!found)
                {
                    bishop_distances[i] = Tuple.Create(2, 0);
                }
            }
        }
        #endregion

        #region Knight Distances
        //Find Knight Distances
        var X = new int[] { 2, 2, -2, -2, 1, 1, -1, -1 };
        var Y = new int[] { -1, 1, -1, 1, 2, -2, 2, -2 };

        var Knight_distances = new Tuple<int, int>[(N * N) + 1];
        Knight_distances[1] = Tuple.Create(0, 0);

        for (int i = 2; i <= N * N; i++)
        {
            var current_location = locations[i - 1];
            var new_location = locations[i];

            var list = new List<Tuple<int, int>>();

            bool found = false;

            for (int t = 0; t < 8; t++)
            {
                var new_X = current_location.Item1 + X[t];
                var new_Y = current_location.Item2 + Y[t];
                if (new_X == new_location.Item1 && new_Y == new_location.Item2)
                {
                    Knight_distances[i] = Tuple.Create(1, 0);
                    found = true;
                    break;
                }
                else
                {
                    if (new_X >= 1 && new_X <= N && new_Y >= 1 && new_Y <= N)
                        list.Add(Tuple.Create(new_X, new_Y));
                }
            }

            if (!found)
            {
                for (int l = 0; l < list.Count; l++)
                {
                    for (int t = 0; t < 8; t++)
                    {
                        var new_X = list[l].Item1 + X[t];
                        var new_Y = list[l].Item2 + Y[t];
                        if (new_X == new_location.Item1 && new_Y == new_location.Item2)
                        {
                            Knight_distances[i] = Tuple.Create(2, 0);
                            found = true;
                            break;
                        }
                    }
                }
            }

            if (!found) Knight_distances[i] = Tuple.Create(int.MaxValue, 0);

        }
        #endregion

        var minheap = new MinHeap<Move>();

        var states = new Move[3][];
        states[0] = new Move[(N * N) + 1];
        states[1] = new Move[(N * N) + 1];
        states[2] = new Move[(N * N) + 1];

        for (int j = 1; j <= N * N; j++)
            states[0][j] = new Move() { pawn = Pawn.Bishop, Num = j, Moves = int.MaxValue, Rep = int.MaxValue };
        for (int j = 1; j <= N * N; j++)
            states[1][j] = new Move() { pawn = Pawn.Knight, Num = j, Moves = int.MaxValue, Rep = int.MaxValue };
        for (int j = 1; j <= N * N; j++)
            states[2][j] = new Move() { pawn = Pawn.Rook, Num = j, Moves = int.MaxValue, Rep = int.MaxValue };

        states[0][1] = new Move() { pawn = Pawn.Bishop, Num = 1, Moves = 0, Rep = 0 };
        states[1][1] = new Move() { pawn = Pawn.Knight, Num = 1, Moves = 0, Rep = 0 };
        states[2][1] = new Move() { pawn = Pawn.Rook, Num = 1, Moves = 0, Rep = 0 };

        minheap.Add(states[0][1]);
        minheap.Add(states[1][1]);
        minheap.Add(states[2][1]);

        while (minheap.Count > 0)
        {
            var item = minheap.ExtractMin();

            Pawn p = item.pawn;
            int currentnum = item.Num;
            int intPaws = (int)p;

            Console.WriteLine("*******************");
            Console.WriteLine("" + p + " currentnum " + currentnum);

            if (currentnum == N * N) break;

            if (p == Pawn.Bishop)
            {
                //Get Bishop distance
                var dist = bishop_distances[currentnum + 1];
                if (dist.Item1 != int.MaxValue)
                {
                    if (item.Moves + dist.Item1 < states[0][currentnum + 1].Moves
                        || (item.Moves + dist.Item1 == states[0][currentnum + 1].Moves && item.Rep < states[0][currentnum + 1].Rep))
                    {
                        states[0][currentnum + 1] = new Move()
                        {
                            Num = currentnum + 1,
                            Moves = item.Moves + dist.Item1,
                            pawn = Pawn.Bishop,
                            Rep = item.Rep
                        };

                        minheap.Add(states[0][currentnum + 1]);
                    }
                }

                //Get Knight distance
                dist = Knight_distances[currentnum + 1];
                if (dist.Item1 != int.MaxValue)
                {
                    if (item.Moves + dist.Item1 + 1 < states[1][currentnum + 1].Moves
                        || (item.Moves + dist.Item1 + 1 == states[1][currentnum + 1].Moves && item.Rep + 1 < states[1][currentnum + 1].Rep))
                    {
                        states[1][currentnum + 1] = new Move()
                        {
                            Num = currentnum + 1,
                            Moves = item.Moves + dist.Item1 + 1,
                            pawn = Pawn.Knight,
                            Rep = item.Rep + 1
                        };

                        minheap.Add(states[1][currentnum + 1]);
                    }
                }

                //Get Rook distance
                dist = rook_distances[currentnum + 1];
                if (dist.Item1 != int.MaxValue)
                {
                    if (item.Moves + dist.Item1 + 1 < states[2][currentnum + 1].Moves
                        || (item.Moves + dist.Item1 + 1 == states[2][currentnum + 1].Moves && item.Rep + 1 < states[2][currentnum + 1].Rep))
                    {
                        states[2][currentnum + 1] = new Move()
                        {
                            Num = currentnum + 1,
                            Moves = item.Moves + dist.Item1 + 1,
                            pawn = Pawn.Rook,
                            Rep = item.Rep + 1
                        };

                        minheap.Add(states[2][currentnum + 1]);
                    }
                }
            }
            else if (p == Pawn.Knight)
            {
                var dist = Knight_distances[currentnum + 1];
                if (dist.Item1 != int.MaxValue)
                {
                    if (item.Moves + dist.Item1 < states[1][currentnum + 1].Moves
                        || (item.Moves + dist.Item1 == states[1][currentnum + 1].Moves && item.Rep < states[1][currentnum + 1].Rep))
                    {
                        states[1][currentnum + 1] = new Move()
                        {
                            Num = currentnum + 1,
                            Moves = item.Moves + dist.Item1,
                            pawn = Pawn.Knight,
                            Rep = item.Rep
                        };

                        minheap.Add(states[1][currentnum + 1]);
                    }
                }

                dist = rook_distances[currentnum + 1];
                if (dist.Item1 != int.MaxValue)
                {
                    if (item.Moves + dist.Item1 + 1 < states[2][currentnum + 1].Moves
                        || (item.Moves + dist.Item1 + 1 == states[2][currentnum + 1].Moves && item.Rep + 1 < states[2][currentnum + 1].Rep))
                    {
                        states[2][currentnum + 1] = new Move()
                        {
                            Num = currentnum + 1,
                            Moves = item.Moves + dist.Item1 + 1,
                            pawn = Pawn.Rook,
                            Rep = item.Rep + 1
                        };

                        minheap.Add(states[2][currentnum + 1]);
                    }
                }

                dist = bishop_distances[currentnum + 1];
                if (dist.Item1 != int.MaxValue)
                {
                    if (item.Moves + dist.Item1 + 1 < states[0][currentnum + 1].Moves
                        || (item.Moves + dist.Item1 + 1 == states[0][currentnum + 1].Moves && item.Rep + 1 < states[0][currentnum + 1].Rep))
                    {
                        states[0][currentnum + 1] = new Move()
                        {
                            Num = currentnum + 1,
                            Moves = item.Moves + dist.Item1 + 1,
                            pawn = Pawn.Bishop,
                            Rep = item.Rep + 1
                        };

                        minheap.Add(states[0][currentnum + 1]);
                    }
                }
            }
            else if (p == Pawn.Rook)
            {
                var dist = rook_distances[currentnum + 1];
                if (dist.Item1 != int.MaxValue)
                {
                    if (item.Moves + dist.Item1 < states[2][currentnum + 1].Moves
                        || (item.Moves + dist.Item1 == states[2][currentnum + 1].Moves && item.Rep < states[2][currentnum + 1].Rep))
                    {
                        states[2][currentnum + 1] = new Move()
                        {
                            Num = currentnum + 1,
                            Moves = item.Moves + dist.Item1,
                            pawn = Pawn.Rook,
                            Rep = item.Rep
                        };

                        minheap.Add(states[2][currentnum + 1]);
                    }
                }

                dist = bishop_distances[currentnum + 1];
                if (dist.Item1 != int.MaxValue)
                {
                    if (item.Moves + dist.Item1 + 1 < states[0][currentnum + 1].Moves
                        || (item.Moves + dist.Item1 + 1 == states[0][currentnum + 1].Moves && item.Rep + 1 < states[0][currentnum + 1].Rep))
                    {
                        states[0][currentnum + 1] = new Move()
                        {
                            Num = currentnum + 1,
                            Moves = item.Moves + dist.Item1 + 1,
                            pawn = Pawn.Bishop,
                            Rep = item.Rep + 1
                        };

                        minheap.Add(states[0][currentnum + 1]);
                    }
                }

                dist = Knight_distances[currentnum + 1];
                if (dist.Item1 != int.MaxValue)
                {
                    if (item.Moves + dist.Item1 + 1 < states[2][currentnum + 1].Moves
                        || (item.Moves + dist.Item1 + 1 == states[2][currentnum + 1].Moves && item.Rep + 1 < states[2][currentnum + 1].Rep))
                    {
                        states[2][currentnum + 1] = new Move()
                        {
                            Num = currentnum + 1,
                            Moves = item.Moves + dist.Item1 + 1,
                            pawn = Pawn.Knight,
                            Rep = item.Rep + 1
                        };

                        minheap.Add(states[2][currentnum + 1]);
                    }
                }
            }
        }

        int res = int.MaxValue;

        res = Math.Min(states[1][N * N].Moves, res);
        res = Math.Min(states[2][N * N].Moves, res);
        res = Math.Min(states[0][N * N].Moves, res);

        int moves = int.MaxValue;

        if (res == states[0][N * N].Moves)
            moves = Math.Min(states[0][N * N].Rep, moves);

        if (res == states[1][N * N].Moves)
            moves = Math.Min(states[1][N * N].Rep, moves);
        if (res == states[2][N * N].Moves)
            moves = Math.Min(states[2][N * N].Rep, moves);

        Console.WriteLine("" + res + " " + moves);
    }
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using static System.Math;

namespace Olymp
{
  public class ProblemSolver
  {
    private Tokenizer input;

    public void Solve()
    {
      var f = new char[9][];
      ReadField(f);
      var x = input.ReadInt() - 1;
      var y = input.ReadInt() - 1;
      var fx = x % 3;
      var fy = y % 3;
      if (!TryFillField(f, fx, fy))
        FillAll(f);
      PrintField(f);
    }

    private void ReadField(char[][] f)
    {
      for (int i = 0; i < 9; i++)
      {
        if (i != 0 && i % 3 == 0)
          input.ReadLine();
        f[i] = string.Join("", input.ReadLine().Split()).ToCharArray();
      }
    }

    private void PrintField(char[][] f)
    {
      var sb = new StringBuilder();
      for (int i = 0; i < 9; i++)
      {
        if (i != 0 && i % 3 == 0)
          sb.AppendLine();
        for (int j = 0; j < 9; j++)
        {
          if (j != 0 && j % 3 == 0)
            sb.Append(' ');
          sb.Append(f[i][j]);
        }
        sb.AppendLine();
      }
      Console.Write(sb);
    }

    private void FillAll(char[][] f)
    {
      for (int i = 0; i < 9; i++)
        for (int j = 0; j < 9; j++)
          if (f[i][j] == '.')
            f[i][j] = '!';
    }

    private bool TryFillField(char[][] f, int fx, int fy)
    {
      bool result = false;
      for (int i = 0; i < 3; i++)
      {
        for (int j = 0; j < 3; j++)
        {
          int lx = fx * 3 + i;
          int ly = fy * 3 + j;
          if (f[lx][ly] == '.')
          {
            f[lx][ly] = '!';
            result = true;
          }
        }
      }
      return result;
    }

    public ProblemSolver(TextReader input)
    {
      this.input = new Tokenizer(input);
    }

    #region Service functions

    private const string ABC = "abcdefghijklmnopqrstuvwxyz";

    private static void AddCount<T>(Dictionary<T, int> counter, T item)
    {
      int count;
      if (counter.TryGetValue(item, out count))
        counter[item] = count + 1;
      else
        counter.Add(item, 1);
    }

    private static int GetCount<T>(Dictionary<T, int> counter, T item)
    {
      int count;
      return counter.TryGetValue(item, out count) ? count : 0;
    }

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

    #endregion
  }

  #region Service classes

  public class Tokenizer
  {
    private TextReader reader;

    public string ReadToEnd()
    {
      return this.reader.ReadToEnd();
    }

    public int ReadInt()
    {
      var c = SkipWS();
      if (c == -1)
        throw new EndOfStreamException();
      bool isNegative = false;
      if (c == '-' || c == '+')
      {
        isNegative = c == '-';
        c = this.reader.Read();
        if (c == -1)
          throw new InvalidOperationException();
      }
      if (!char.IsDigit((char)c))
        throw new InvalidOperationException();
      int result = (char)c - '0';
      c = this.reader.Read();
      while (c > 0 && !char.IsWhiteSpace((char)c))
      {
        if (!char.IsDigit((char)c))
          throw new InvalidOperationException();
        result = result * 10 + (char)c - '0';
        c = this.reader.Read();
      }
      if (isNegative)
        result = -result;
      return result;
    }

    public string ReadLine()
    {
      return reader.ReadLine();
    }

    public long ReadLong()
    {
      return long.Parse(this.ReadToken());
    }

    public double ReadDouble()
    {
      return double.Parse(this.ReadToken(), CultureInfo.InvariantCulture);
    }

    public int[] ReadIntArray(int n)
    {
      int[] a = new int[n];
      for (int i = 0; i < n; i++)
        a[i] = ReadInt();
      return a;
    }

    public long[] ReadLongArray(int n)
    {
      long[] a = new long[n];
      for (int i = 0; i < n; i++)
        a[i] = ReadLong();
      return a;
    }

    public double[] ReadDoubleArray(int n)
    {
      double[] a = new double[n];
      for (int i = 0; i < n; i++)
        a[i] = ReadDouble();
      return a;
    }

    public string ReadToken()
    {
      var c = SkipWS();
      if (c == -1)
        return null;
      var sb = new StringBuilder();
      while (c > 0 && !char.IsWhiteSpace((char)c))
      {
        sb.Append((char)c);
        c = this.reader.Read();
      }
      return sb.ToString();
    }

    private int SkipWS()
    {
      int c = this.reader.Read();
      if (c == -1)
        return c;
      while (c > 0 && char.IsWhiteSpace((char)c))
        c = this.reader.Read();
      return c;
    }

    public Tokenizer(TextReader reader)
    {
      this.reader = reader;
    }
  }

  class Program
  {
    public static void Main(string[] args)
    {
      var solver = new ProblemSolver(Console.In);
      solver.Solve();
    }
  }

  #endregion
}


using System;
using System.Collections.Generic;

namespace Codeforces {
	class MyClass {
		public static void Main (string[] args)
		{
			System.Threading.Thread.CurrentThread.CurrentUICulture
				= new System.Globalization.CultureInfo("ja-JP");
			string[] ss = Console.ReadLine().Split(' ');
			int n = int.Parse(ss[0]);
			int m = int.Parse(ss[1]);
			int s = int.Parse(ss[2]);
			long ans = 0;
			ans = ((m-1)/s+1) * ((m-1)%s+1);
			ans *= ((n-1)/s+1) * ((n-1)%s+1);
			Console.WriteLine (ans);
		}
	}
}


using System;
namespace Iran{
        class amir
        {
                public static void Main()
                {
                        int x,y,s;
                        string[] input=Console.ReadLine().Split(' ');
                        x=Int32.Parse(input[0]);
                        y=Int32.Parse(input[1]);
                        s=Int32.Parse(input[2]);
                        long yek,dow;
                        if(x%s==0)
                                yek=x;
                        else
                        {
                                yek=(x%s)*(x/s+1);
                        }
                        if(y%s==0)
                                dow=y;
                        else
                        {
                                dow=(y%s)*(y/s+1);
                        }
                        long ans=dow*yek;
                        Console.WriteLine(ans);

                }
        }
}


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

class Program
{

    void solve()
    {
        long n = nextInt();
        long m = nextInt();
        long s = nextInt();
        long res = get(n, s);
        res *= get(m, s);
        println(res);
    }

    private long get(long n, long s)
    {
        long most = (n - 1) / s;
        int res = 0;
        for (int i = 0; i < n; i++)
        {
            long cur = (n - 1 - i) / s;
            cur += i / s;
            if (cur == most)
                res++;
        }
        return res;
    }

    ////////////
    private void println(int[] ar)
    {
        for (int i = 0; i < ar.Length; i++)
        {
            if (i == ar.Length - 1)
                println(ar[i]);
            else
                print(ar[i] + " ");
        }
    }
    private void println(int[] ar, bool add)
    {
        int A = 0;
        if (add)
            A++;
        for (int i = 0; i < ar.Length; i++)
        {
            if (i == ar.Length - 1)
                println(ar[i] + A);
            else
                print((ar[i] + A) + " ");
        }
    }

    private void println(string Stringst)
    {
        Console.WriteLine(Stringst);
    }
    private void println(char charnum)
    {
        Console.WriteLine(charnum);
    }
    private void println(int Intnum)
    {
        Console.WriteLine(Intnum);
    }
    private void println(long Longnum)
    {
        Console.WriteLine(Longnum);
    }
    private void println(double Doublenum)
    {
        string s = Doublenum.ToString(CultureInfo.InvariantCulture);
        Console.WriteLine(s);
    }

    private void print(string Stringst)
    {
        Console.Write(Stringst);
    }
    private void print(int Intnum)
    {
        Console.Write(Intnum);
    }
    private void print(char charnum)
    {
        Console.Write(charnum);
    }
    private void print(long Longnum)
    {
        Console.Write(Longnum);
    }
    private void print(double Doublenum)
    {
        Console.Write(Doublenum);
    }

    string[] inputLine = new string[0];
    int inputInd = 0;
    string nextLine()
    {
        return Console.ReadLine();
    }
    void readInput()
    {
        if (inputInd != inputLine.Length)
            throw new Exception();
        inputInd = 0;
        inputLine = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
        if (inputLine.Length == 0)
            readInput();

    }
    int nextInt()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return int.Parse(inputLine[inputInd++]);
    }
    long nextLong()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return long.Parse(inputLine[inputInd++]);
    }
    double nextDouble()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return double.Parse(inputLine[inputInd++]);
    }
    string nextString()
    {
        if (inputInd == inputLine.Length)
            readInput();
        return inputLine[inputInd++];
    }
    static void Main(string[] args)
    {
        new Program().solve();
    }
}


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

namespace A
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] t_A = Console.ReadLine().Split(' ');
            string[] t_B = Console.ReadLine().Split(' ');
            string[] HHH = Console.ReadLine().Split(':');
           int hour = 0;
           if (HHH[0][0] == '0') hour = int.Parse(HHH[0][1].ToString());
           else
               hour = int.Parse(HHH[0][0].ToString()) * 10 + int.Parse(HHH[0][1].ToString());
           int minute = 0;
           if (HHH[1][0] == '0') minute = int.Parse(HHH[1][1].ToString());
           else
               minute = int.Parse(HHH[1][0].ToString()) * 10 + int.Parse(HHH[1][1].ToString());
           int starttime = hour * 60 + minute;
           int endtime = starttime + int.Parse(t_A[1].ToString());
           int ans = 0;
           int start = 300;
           int chastota = int.Parse(t_B[0].ToString());
           int time = int.Parse(t_B[1].ToString());
           while (true)
           {
               if (start>=endtime) break;
               if(start+time>starttime)ans++;
               start += chastota;
           }

           Console.WriteLine(ans);

        }
    }
}


using System;

class Solution
{
	static void Main()
	{
		var inputs = Console.ReadLine().Split();
		var a = int.Parse(inputs[0]);
		var ta = int.Parse(inputs[1]);
		inputs = Console.ReadLine().Split();
		var b = int.Parse(inputs[0]);
		var tb = int.Parse(inputs[1]);
		inputs = Console.ReadLine().Split(':');
		var hh = int.Parse(inputs[0]);
		var mm = int.Parse(inputs[1]);

		var ts = hh*60 + mm;
		Console.WriteLine(GetNumberOfDepartedBuses(ts-tb, ts+ta, b));
	}

	//Returns number of departed buses in the given interval, if inteval ends are exclusive, and
	static int GetNumberOfDepartedBuses(int intervalStart, int intervalEnd, int departureFrequency, int firstDeparture = 5*60, int lastDeparture = 23*60+59)
	{
		int numUntilStart = intervalStart >= firstDeparture
			? (intervalStart - firstDeparture) / departureFrequency + 1	//Inclusive-Inclusive
			: 0;
		int numUntilEnd = intervalEnd <= lastDeparture
			? ((intervalEnd - 1) - firstDeparture) / departureFrequency + 1	//Inclusive-Exclusive
			: (lastDeparture - firstDeparture) / departureFrequency + 1;	//Inclusive-Inclusive

		return numUntilEnd - numUntilStart;
	}
}


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

namespace A
{
    class Program
    {
        static void Main(string[] args)
        {

            string[] t_A = Console.ReadLine().Split(' ');
            string[] t_B = Console.ReadLine().Split(' ');
            string[] HHH = Console.ReadLine().Split(':');
            int hour = 0;
            if (HHH[0][0] == '0') hour = int.Parse(HHH[0][1].ToString());
            else
                hour = int.Parse(HHH[0][0].ToString()) * 10 + int.Parse(HHH[0][1].ToString());
            int minute = 0;
            if (HHH[1][0] == '0') minute = int.Parse(HHH[1][1].ToString());
            else
                minute = int.Parse(HHH[1][0].ToString()) * 10 + int.Parse(HHH[1][1].ToString());
            int starttime = hour * 60 + minute;
            int endtime = starttime + int.Parse(t_A[1].ToString());
            int ans = 0;
            int start = 300;
            int chastota = int.Parse(t_B[0].ToString());
            int time = int.Parse(t_B[1].ToString());
            while (true)
            {
                if (start >= endtime || start > 1439) break;
                if (start + time > starttime)
                {
                    ans++;
                }
                start += chastota;
            }

            Console.WriteLine(ans);

        }
    }
}


#if DEBUG
using System.Reflection;
using System.Threading.Tasks;
#endif

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

namespace _12a
{
    class Program
    {
        static string _inputFilename = "input.txt";
        static string _outputFilename = "output.txt";
        static bool _useFileInput = false;

        static void Main(string[] args)
        {
            StreamWriter file = null;
#if DEBUG
			Console.SetIn(getInput());
#else
            if (_useFileInput)
            {
                Console.SetIn(File.OpenText(_inputFilename));
                file = new StreamWriter(_outputFilename);
                Console.SetOut(file);
            }
#endif

            try
            {
                solution();
            }
            finally
            {
                if (_useFileInput)
                {
                    file.Close();
                }
            }
        }

        static void solution()
        {
            #region SOLUTION
            var d = readIntArray();
            var a = d[0];
            var ta = d[1];

            d = readIntArray();
            var b = d[0];
            var tb = d[1];

            var f = Console.ReadLine().Split(':').Select(k => int.Parse(k)).ToArray();
            var hh = f[0];
            var mm = f[1];

            var dt = hh * 60 + mm;
            var at = dt + ta;
            var next = 5 * 60;
            var c = 0;
            var fin = 23 * 60 + 59;
            while (true)
            {
                if (next > fin)
                {
                    break;
                }

                if (next >= dt && next < at)
                {
                    c++;
                }

                if (next < dt && next + tb > dt)
                {
                    c++;
                }

                next += b;
            }

            Console.WriteLine(c);

            //var n = readInt();
            //var m = readInt();

            //var a = readIntArray();
            //var b = readIntArray();
            #endregion
        }

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

        static long readLong()
        {
            return long.Parse(Console.ReadLine());
        }

        static int[] readIntArray()
        {
            return Console.ReadLine().Split(' ').Where(i => !string.IsNullOrEmpty(i)).Select(i => int.Parse(i)).ToArray();
        }

        static long[] readLongArray()
        {
            return Console.ReadLine().Split(' ').Where(i => !string.IsNullOrEmpty(i)).Select(i => long.Parse(i)).ToArray();
        }

#if DEBUG
		static StreamReader getInput()
		{
			var resName = Assembly.GetCallingAssembly().GetManifestResourceNames()[0];
			var resource = Assembly.GetCallingAssembly().GetManifestResourceStream(resName);

			return new StreamReader(resource);
		}
#endif
    }
}


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

class Program
{

    void solve()
    {
        string t = Console.ReadLine();
        int n = t.Length;
        bool[,] vis = new bool[2 * n + 1, 2 * n + 1];
        int x = n;
        int y = n;
        vis[x, y] = true;
        bool ok = true;
        int ex = -1;
        int ey = -1;
        for (int i = 0; i < t.Length; i++)
        {
            int nx, ny;
            nx=x;
            ny=y;
            if (t[i] == 'D')
                ny--;
            if (t[i] == 'U')
                ny++;
            if (t[i] == 'L')
                nx--;
            if (t[i] == 'R')
                nx++;
            vis[nx, ny] = true;
            x = nx;
            y = ny;
            ex = x;
            ey = y;

        }
        if (!ok)
            ;
        else
        {
            int[,] d = new int[2 * n + 1, 2 * n + 1];
            d[n, n] = 0;
            for (int i = 0; i < 2 * n + 1; i++)
                for (int j = 0; j < 2 * n+1; j++)
                    if (i != n || j != n)
                        d[i, j] = -1;
            Queue<int> q = new Queue<int>();
            x = n;
            y = n;
            q.Enqueue(x);
            q.Enqueue(y);
            while (q.Count > 0)
            {
                x = q.Dequeue();
                y = q.Dequeue();
                for (int dx = -1; dx <= 1; dx++)
                    for (int dy = -1; dy <= 1; dy++)
                        if (dx == 0 ^ dy == 0)
                        {
                            int nx = x + dx;
                            int ny = y + dy;
                            if (nx < 0 || nx > 2 * n || ny < 0 || ny > 2 * n || !vis[nx, ny] || d[nx, ny] != -1)
                                continue;
                            d[nx, ny] = 1 + d[x, y];
                            q.Enqueue(nx);
                            q.Enqueue(ny);

                        }
            }
            if (d[ex, ey] == t.Length)
                Console.WriteLine("OK");
            else
                Console.WriteLine("BUG");
        }
       // Console.ReadLine();
    }

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


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

namespace Main
{
    class Program
    {
        public static void Main(String[] args)
        {
//            Console.SetIn(new StreamReader("c:\\a.txt"));

            var movementString = Console.ReadLine().ToCharArray();

            HashSet<Tuple<int, int>> pos = new HashSet<Tuple<int, int>>();

            Console.WriteLine(IsShortestPathCandidate(movementString, 0, pos, 0, 0) ? "OK" : "BUG");
        }

        private static bool IsShortestPathCandidate(char[] movementString, int i, HashSet<Tuple<int, int>> pos, int x, int y)
        {
            if (pos.Contains(new Tuple<int, int>(x, y)))
                return false;

            if (AdjacentVisitedPoints(x, y, pos) > 1)
                return false;

            if (i == movementString.Length)
                return true;

            pos.Add(new Tuple<int, int>(x, y));

            switch (movementString[i])
            {
                case 'L':
                    return IsShortestPathCandidate(movementString, i + 1, pos, x, y - 1);
                case 'R':
                    return IsShortestPathCandidate(movementString, i + 1, pos, x, y + 1);
                case 'U':
                    return IsShortestPathCandidate(movementString, i + 1, pos, x - 1, y);
                default:
                    return IsShortestPathCandidate(movementString, i + 1, pos, x + 1, y);
            }
        }

        private static int AdjacentVisitedPoints(int x, int y, HashSet<Tuple<int, int>> pos)
        {
            int vis = 0;

            if (pos.Contains(new Tuple<int, int>(x + 1, y)))
                vis++;

            if (pos.Contains(new Tuple<int, int>(x - 1, y)))
                vis++;

            if (pos.Contains(new Tuple<int, int>(x, y + 1)))
                vis++;

            if (pos.Contains(new Tuple<int, int>(x, y - 1)))
                vis++;

            return vis;
        }
    }
}


using System;
using System.Linq;
using System.IO;
using System.Collections.Generic;
// Powered by caide (code generator, tester, and library code inliner)

class Solution {
    public void solve(TextReader input, TextWriter output) {
        string steps = input.ReadLine();

        int n = steps.Length * 2 + 3;
        int[,] map = new int[n, n];

        int x = n / 2;
        int y = x;
        map[x, y] = 1;
        foreach (char step in steps)
        {
            if (step == 'L')
                x--;
            if (step == 'R')
                x++;
            if (step == 'U')
                y++;
            if (step == 'D')
                y--;
            map[x, y]++;

            int val =           map[x, y - 1] +
                map[x - 1, y] + map[x, y]     + map[x + 1, y]
                              + map[x, y + 1];
            if(val > 2)
            {
                output.WriteLine("BUG");
                return;
            }
        }

        output.WriteLine("OK");
    }
}

class CaideConstants {
    public const string InputFile = null;
    public const string OutputFile = null;
}
public class Program {
    public static void Main(string[] args) {
        Solution solution = new Solution();
        using (System.IO.TextReader input =
                CaideConstants.InputFile == null ? System.Console.In :
                        new System.IO.StreamReader(CaideConstants.InputFile))
        using (System.IO.TextWriter output =
                CaideConstants.OutputFile == null ? System.Console.Out:
                        new System.IO.StreamWriter(CaideConstants.OutputFile))

            solution.solve(input, output);
    }
}


#if DEBUG
using System.Reflection;
using System.Threading.Tasks;
#endif

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

namespace _8b
{
    class Program
    {
        static string _inputFilename = "input.txt";
        static string _outputFilename = "output.txt";
        static bool _useFileInput = false;

        static void Main(string[] args)
        {
            StreamWriter file = null;
#if DEBUG
			Console.SetIn(getInput());
#else
            if (_useFileInput)
            {
                Console.SetIn(File.OpenText(_inputFilename));
                file = new StreamWriter(_outputFilename);
                Console.SetOut(file);
            }
#endif

            try
            {
                solution();
            }
            finally
            {
                if (_useFileInput)
                {
                    file.Close();
                }
            }
        }

        static void solution()
        {
            #region SOLUTION
            var s = Console.ReadLine();
            var n = s.Length;
            var t = new int[2 * n + 5, 2 * n + 5];
            var x = n;
            var y = n;
            t[y, x] = 1;
            for (int i = 0; i < n; i++)
            {
                var next = s[i];
                if (next == 'L')
                {
                    y--;
                    if (t[y, x] == 1)
                    {
                        Console.WriteLine("BUG");
                        return;
                    }
                    else
                    {

                        t[y, x] = 1;
                        t[y + 1, x - 1] = 1;
                        t[y + 1, x + 1] = 1;
                        t[y + 1 - 1, x] = 1;
                        t[y + 1 + 1, x] = 1;
                    }
                }
                else if (next == 'R')
                {
                    y++;
                    if (t[y, x] == 1)
                    {
                        Console.WriteLine("BUG");
                        return;
                    }
                    else
                    {

                        t[y, x] = 1;
                        t[y - 1, x - 1] = 1;
                        t[y - 1, x + 1] = 1;
                        t[y - 1 - 1, x] = 1;
                        t[y - 1 + 1, x] = 1;
                    }
                }
                else if (next == 'U')
                {
                    x++;
                    if (t[y, x] == 1)
                    {
                        Console.WriteLine("BUG");
                        return;
                    }
                    else
                    {

                        t[y, x] = 1;
                        t[y, x - 1 - 1] = 1;
                        t[y , x + 1 - 1] = 1;
                        t[y - 1, x - 1] = 1;
                        t[y + 1, x - 1] = 1;
                    }
                }
                else if (next == 'D')
                {
                    x--;
                    if (t[y, x] == 1)
                    {
                        Console.WriteLine("BUG");
                        return;
                    }
                    else
                    {

                        t[y, x] = 1;
                        t[y, x - 1 + 1] = 1;
                        t[y, x + 1 + 1] = 1;
                        t[y - 1, x + 1] = 1;
                        t[y + 1, x + 1] = 1;
                    }
                }
            }

            Console.WriteLine("OK");

            //var n = readInt();
            //var m = readInt();

            //var a = readIntArray();
            //var b = readIntArray();
            #endregion
        }

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

        static long readLong()
        {
            return long.Parse(Console.ReadLine());
        }

        static int[] readIntArray()
        {
            return Console.ReadLine().Split(' ').Where(i => !string.IsNullOrEmpty(i)).Select(i => int.Parse(i)).ToArray();
        }

        static long[] readLongArray()
        {
            return Console.ReadLine().Split(' ').Where(i => !string.IsNullOrEmpty(i)).Select(i => long.Parse(i)).ToArray();
        }

#if DEBUG
		static StreamReader getInput()
		{
			var resName = Assembly.GetCallingAssembly().GetManifestResourceNames()[0];
			var resource = Assembly.GetCallingAssembly().GetManifestResourceStream(resName);

			return new StreamReader(resource);
		}
#endif
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            bool[,] map;

            string s = Console.ReadLine();
            int l, r, u, d;
            int x, X, y, Y;
            x = X = y = Y = 0;
            l = r = u = d = 0;
            foreach (char c in s)
            {
                switch (c)
                {
                    case('L'):
                        l++;
                        break;
                    case('R'):
                        l--;
                        break;
                    case('U'):
                        u++;
                        break;
                    case('D'):
                        u--;
                        break;
                }
                if (l > X)
                {
                    X = l;
                }
                if (l < x) x = l;
                if (u < y) y = u;
                if (u > Y) Y = u;

            }
            map = new bool[Y - y+1, X - x+1];
            int ly=Y-y+1;
            int lx=X-x+1;
            int startx = X;
            int starty = Y;
            int i = starty;
            int j = startx;
            foreach (char c in s)
            {
                map[i, j] = true;
                switch (c)
                {
                    case ('L'):
                        j--;
                        break;
                    case ('R'):
                        j++;
                        break;
                    case ('U'):
                        i--;
                        break;
                    case ('D'):
                        i++;
                        break;
                }
            }
            map[i,j]=true;
            int[,] a = new int[ly, lx];
            for (int k = 0; k<ly; k++)
            {
                for (int m = 0; m < lx; m++)
                {
                    a[k, m] = int.MaxValue;
                }
            }
            a[starty, startx] = 0;
            findRoad(a, map, starty, startx, 0, lx, ly);
            if (a[i, j] == s.Length)
            {
                Console.WriteLine("OK");
            }
            else
            {
                Console.WriteLine("BUG");
            }
        }
        public static void findRoad(int[,] a, bool[,] map, int i, int j, int dist, int lx, int ly)
        {
            if (a[i, j] >= dist)
            {
                a[i, j] = dist;
                if (i + 1 < ly && map[i + 1, j]) findRoad(a, map, i + 1, j, a[i, j] + 1, lx, ly);
                if (i > 0 && map[i - 1, j]) findRoad(a, map, i - 1, j, a[i, j] + 1, lx, ly);
                if (j + 1 < lx && map[i, j + 1]) findRoad(a, map, i, j + 1, a[i, j] + 1, lx, ly);
                if (j > 0 && map[i, j - 1]) findRoad(a, map, i, j - 1, a[i, j] + 1, lx, ly);
            }
        }
    }
}


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

namespace NS
{
	class MainClass
	{
		static List<int> xa, ya;
		public static void Main (string[] args)
		{
			var s = Console.ReadLine ();

			xa = new List<int> ();
			ya = new List<int> ();
			int x = 0;
			int y = 0;
			xa.Add(0);
			ya.Add(0);
			foreach (var c in s) {
				if (c=='L')
					x--;
				if (c=='R')
					x++;
				if (c=='U')
					y++;
				if (c=='D')
					y--;

				for(int i = 0; i<xa.Count-1; i++) {
					if ((Math.Abs(xa[i]-x)<=1 && ya[i]-y==0) || (Math.Abs(ya[i]-y)<=1 && xa[i]-x==0)) {
						Console.WriteLine("BUG");
						return;
					}
				}

				xa.Add(x);
				ya.Add(y);
			}

			Console.WriteLine("OK");
		}
	}
}


/*******************************************************************************
* Author: Nirushuu
*******************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.IO;
using System.Globalization;
using System.Numerics;

/*******************************************************************************
* IO from Kattio.cs from open.kattis.com/help/csharp                          */
public class NoMoreTokensException : Exception
{
}

public class Tokenizer
{
    string[] tokens = new string[0];
    private int pos;
    StreamReader reader;

    public Tokenizer(Stream inStream)
    {
        var bs = new BufferedStream(inStream);
        reader = new StreamReader(bs);
    }

    public Tokenizer() : this(Console.OpenStandardInput())
    {
        // Nothing more to do
    }

    private string PeekNext()
    {
        if (pos < 0)
            // pos < 0 indicates that there are no more tokens
            return null;
        if (pos < tokens.Length)
        {
            if (tokens[pos].Length == 0)
            {
                ++pos;
                return PeekNext();
            }
            return tokens[pos];
        }
        string line = reader.ReadLine();
        if (line == null)
        {
            // There is no more data to read
            pos = -1;
            return null;
        }
        // Split the line that was read on white space characters
        tokens = line.Split(null);
        pos = 0;
        return PeekNext();
    }

    public bool HasNext()
    {
        return (PeekNext() != null);
    }

    public string Next()
    {
        string next = PeekNext();
        if (next == null)
            throw new NoMoreTokensException();
        ++pos;
        return next;
    }
}

public class Scanner : Tokenizer
{

    public int NextInt()
    {
        return int.Parse(Next());
    }

    public long NextLong()
    {
        return long.Parse(Next());
    }

    public float NextFloat()
    {
        return float.Parse(Next());
    }

    public double NextDouble()
    {
        return double.Parse(Next());
    }
}

public class BufferedStdoutWriter : StreamWriter
{
    public BufferedStdoutWriter() : base(new BufferedStream(Console.OpenStandardOutput()))
    {
    }
}
/******************************************************************************/

/*******************************************************************************
* DisjointSet datastructure                                                   */
public struct DisjointSet {
    public int[] parent;
    public int[] rank;
    public DisjointSet(int n) {
        parent = new int[n];
        for (int i = 0; i < n; ++i) {
            parent[i] = i;
        }
        rank = new int[n];
    }

    public int Find(int i) {
        int idx = i;
        var compress = new List<int>();
        while (idx != parent[idx]) {
            compress.Add(idx);
            idx = parent[idx];
        }

        foreach (var e in compress) {   // path compress
            parent[e] = idx;
        }
        return idx;
    }

    public void Union(int a, int b) {
        int aPar = this.Find(a);
        int bPar = this.Find(b);

        if (rank[aPar] > rank[bPar]) {  // by rank
            parent[bPar] = aPar;
        } else {
            parent[aPar] = bPar;
        }
        if (rank[aPar] == rank[bPar]) {
            ++rank[bPar];
        }
    }
}

class MaxHeap {
    List<int> data;

    public MaxHeap() {
        data = new List<int>();
    }

    public void Add(int a) {
        data.Add(a);
        int idx = data.Count - 1;
        int parent = (int) Math.Floor((double) (idx - 1) / 2);
        while (idx != 0 && (data[idx] > data[parent])) {
            int tmp = data[parent];
            data[parent] = data[idx];
            data[idx] = tmp;

            idx = parent;
            parent = (int) Math.Floor((double) (idx - 1) / 2);
        }
    }

    public int Pop() {
        int res = data[0];
        data[0] = data[data.Count - 1];
        data.RemoveAt(data.Count - 1);

        int idx = 0;
        while (true) {
            int child1 = (2 * idx) + 1;
            int child2 = (2 * idx) + 2;

            if (child1 >= data.Count) { // no children
                break;
            } else if (child2 >= data.Count) {  // one child
                if (data[idx] < data[child1]) {
                    int tmp = data[idx];
                    data[idx] = data[child1];
                    data[child1] = tmp;
                }
                break;
            } else {    // two children
                int maxChild = data[child1] > data[child2] ? child1 : child2;
                if (data[idx] < data[maxChild]) {
                    int tmp = data[idx];
                    data[idx] = data[maxChild];
                    data[maxChild] = tmp;
                    idx = maxChild;
                    continue;
                } else {    // no swap
                    break;
                }
            }
        }

        return res;
    }

}

class Dequeue {
    public Dictionary<int, int> data = new Dictionary<int, int>();
    int firstIdx;
    public int Count;
    public Dequeue() {
        data = new Dictionary<int, int>();
        firstIdx = 1;
        Count = 0;
    }

    public void PushFront(int a) {
        data[firstIdx - 1] = a;
        ++Count;
        --firstIdx;
    }

    public void PushBack(int a) {
        data[firstIdx + Count] = a;
        ++Count;
    }

    public int PopFront() {
        --Count;
        ++firstIdx;
        return data[firstIdx - 1];
    }

    public int PopBack() {
        --Count;
        return data[firstIdx + Count];
    }

    public int Get(int n) {
        return data[firstIdx + n];
    }
}
/******************************************************************************/

class MainClass {

    // Debug functions
    static void debug<T>(IEnumerable<T> a, string s = "") {
        Console.WriteLine($"name: {s}");
        int i = 0;
        foreach (var e in a) {
            Console.WriteLine($"value {i}: {e}");
            ++i;
        }
    }
    static void dbg<T>(T a, string s = "") {
        Console.WriteLine($"name: {s} value: {a}");
    }
    ////////////////////

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

    // class PairInt{
    //     public int fst;
    //     public int snd;

    //     // void Add(Pair<int> p) {
    //     //     fst += p.fst;
    //     //     snd += p.snd;
    //     // }
    // }

    public static void Main() {

        var sc = new Scanner();
        var wr = new BufferedStdoutWriter();
        var dot = new NumberFormatInfo();
        dot.NumberDecimalSeparator = ".";

        // int T = sc.NextInt();

        // for (int t = 0; t < T; ++t) {
        // }

        string s = sc.Next();

        var visited = new HashSet<Tuple<int, int>>();
        var dir = new Dictionary<char, Tuple<int, int>>{
            {'R', new Tuple<int, int>(1, 0)},
            {'L', new Tuple<int, int>(-1, 0)},
            {'U', new Tuple<int, int>(0, 1)},
            {'D', new Tuple<int, int>(0, -1)}
        };

        var pos = new Tuple<int, int>(0, 0);
        visited.Add(pos);

        foreach (var e in s) {
            var prev = pos;
            pos = new Tuple<int, int>(pos.Item1 + dir[e].Item1, pos.Item2 + dir[e].Item2);

            visited.Add(pos);

            foreach (var d in dir.Values) {
                var nbr = new Tuple<int, int>(pos.Item1 + d.Item1, pos.Item2 + d.Item2);
                if (visited.Contains(nbr) && !(nbr.Equals(prev))) {
                    // wr.Write(e);
                    // wr.Write(pos);
                    // wr.Write(nbr);
                    wr.Write("BUG");
                    wr.Flush();
                    return;
                }
            }
        }

        // debug(pos);
        // foreach (var e in visited) {
        //     debug(e);
        // }

        wr.Write("OK");

        wr.Flush();

    }
}


using System;
using System.Text;

class Program
{

    void solve()
    {
        string t = Console.ReadLine();
        int n = t.Length;
        bool[,] vis = new bool[2 * n + 1, 2 * n + 1];
        int r = n;
        int c = n;
        vis[r, c] = true;
        bool ok = true;
        for (int i = 0; i < t.Length; i++)
        {
            int nr, nc;
            nr=r;
            nc=c;
            switch (t[i])
            {
                case 'D':
                    nr--;
                    break;
                case 'U':
                    nr++;
                    break;
                case 'L':
                    nc--;
                    break;
                default:
                    nc++;
                    break;
            }
            if (vis[nr, nc])
            {
                ok = false;
                break;
            }
            vis[nr, nc] = true;

        }
        if (ok)
            Console.WriteLine("OK");
        else
            Console.WriteLine("BUG");
    }

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


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

namespace Kids_Riddle
{
    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 n = Next();

            string s = Convert.ToString(n, 16).ToUpper();
            int count = 0;
            foreach (char c in s)
            {
                switch (c)
                {
                    case '0':
                    case '4':
                    case '6':
                    case '9':
                    case 'A':
                    case 'D':
                        count++;
                        break;
                    case '8':
                    case 'B':
                        count++;
                        count++;
                        break;
                }
            }

            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';
            }
        }
    }
}


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            long a, x, y, role, sqr;
            string str;
            string[] split = new string[3];
            str = Console.ReadLine();
            split = str.Split();
            a = Convert.ToInt32(split[0]);
            x = Convert.ToInt32(split[1]);
            y = Convert.ToInt32(split[2]);

            if (y % a == 0)
                Console.WriteLine("-1");
            else
            {
                role = y / a + 1;
                if (y < a)
                {

                    if (Math.Abs(x) < a / 2.0)
                        Console.WriteLine(1);
                }
                else
                {
                    sqr = role % 2 + 1;
                    if (sqr == 1)
                        if (Math.Abs(x) < a / 2)
                            Console.WriteLine((1 + (role - 2) / 2 * 3)+1);
                        else Console.WriteLine("-1");
                    else
                        if ((x>0)&&(x<a))
                            Console.WriteLine((2 + (role - 3) / 2 * 3) +2);
                        else if ((x<0)&&(x>-a))
                            Console.WriteLine((2 + (role - 3) / 2 * 3) + 1);
                        else Console.WriteLine("-1");

                    // 2+(role-3)/2*3
                    //1 + (role-2)/2*3
                }
            }

        }
    }
}


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

namespace Hopscotch
{
    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 a = Next();
            int x = Next();
            int y = Next();

            int count;
            if (y%a == 0)
            {
                writer.WriteLine("-1");
                writer.Flush();
                return;
            }
            int row = (y + a - 1)/a;
            if (row == 1 || row%2 == 0)
            {
                if (2*Math.Abs(x) >= a)
                {
                    writer.WriteLine("-1");
                    writer.Flush();
                    return;
                }
                count = row + (row - 1)/2;
            }
            else
            {
                if (Math.Abs(x) >= a || x == 0)
                {
                    writer.WriteLine("-1");
                    writer.Flush();
                    return;
                }
                count = row + (row - 1)/2;
                if (x < 0)
                    count--;
            }

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

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


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

namespace WarmingUpC_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] numbers = Console.ReadLine().Split(' ');
            int c = int.Parse(numbers[4]), a = int.Parse(numbers[0]), b = int.Parse(numbers[1]), w = int.Parse(numbers[2]), x = int.Parse(numbers[3]);
            double Result = Math.Ceiling((-b + (c - a) * x + 0.0) / (w - x)) + c - a;
            Console.WriteLine(Math.Max(0, Result));
        }
    }
}


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

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] inArray = ReadIntLine();
            int Multiplier = 1000000, Multiplier2 = 100000, Multiplier3 = 10000;
            int a = inArray[0], initialB = inArray[1], w = inArray[2], x = inArray[3], c = inArray[4];
            int Tau = 1, aChange = 0;
            int b = initialB;

            if (b >= x)
            {
                b = b - x;
            }
            else
            {
                b = w - x + b;
                aChange++;
            }

            while (b != initialB)
            {
                if (b >= x)
                {
                    b = b - x;
                }
                else
                {
                    b = w - x + b;
                    aChange++;
                }
                Tau++;
            }
            int Result = 0;

            double steps = (a - c) / (aChange - Tau);
            int ss = Convert.ToInt32(Math.Floor(steps) - 1);
            Result = Tau * ss;
            c = c - Tau * ss;
            a = a - aChange * ss;

            while (c != a)
            {
                if (b >= x)
                {
                    b = b - x;
                }
                else
                {
                    b = w - x + b;
                    a--;
                }
                c--;
                Result++;
            }
            if (Result < 0)
                Result = 0;
            Console.WriteLine(Result);
        }
        public static int[] ReadIntLine()
        {
            string[] temp = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            int[] arr = new int[temp.Length];
            for (int i = 0; i < temp.Length; i++)
                arr[i] = int.Parse(temp[i]);
            return arr;
        }

    }
}


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string []numbers = Console.ReadLine().Split(' ');
            int c = int.Parse(numbers[4]), a = int.Parse(numbers[0]), b = int.Parse(numbers[1]), w = int.Parse(numbers[2]), x = int.Parse(numbers[3]);
            int seconds = 0;
            while (c > a)
            {
                c = c - 1;
                if (b >= x)
                    b = b - x;
                if (b < x)
                {
                    a = a - 1;
                    b = w - (x - b);
                }
                seconds++;
            }
            seconds -= 1;
            Console.WriteLine(seconds);
        }
    }
}


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

namespace ConsoleApplication6
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] inArray = ReadIntLine();
            double a = inArray[0], b = inArray[1], w = inArray[2], x = inArray[3], c = inArray[4];
            double Result = Math.Ceiling((-b + (c - a) * x + 0.0) / (w - x)) + c - a;
            Console.WriteLine(Math.Max(0, Result));
        }
        public static double[] ReadIntLine()
        {
            string[] temp = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
            double[] arr = new double[temp.Length];
            for (int i = 0; i < temp.Length; i++)
                arr[i] = double.Parse(temp[i]);
            return arr;
        }

    }
}


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

namespace Contest
{
    class Program
    {
        static void Main()
        {
            int a1 = Convert.ToInt32(Console.ReadLine());
            int a2 = Convert.ToInt32(Console.ReadLine());
            int k1 = Convert.ToInt32(Console.ReadLine());
            int k2 = Convert.ToInt32(Console.ReadLine());
            int n = Convert.ToInt32(Console.ReadLine());
            int min = Math.Min(a1 + a2, Math.Max(0,n - (a1 * (k1 - 1) + a2 * (k2 - 1))));
            int max = Math.Max(a1 + Math.Max(0,n - a1 * k1) / k2, a2 + Math.Max(0,n - a2 * k2) / k1);
            Console.WriteLine(min + " " + max);
        }

    }

}


using System;
using System.IO;
using System.Collections.Generic;
using System.Numerics;

namespace cf585
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.SetIn(new StreamReader("input.txt"));
            solve_cf585A();
        }

        public static void solve_cf585A()
        {
            int a1 = Convert.ToInt32(Console.ReadLine());
            int a2 = Convert.ToInt32(Console.ReadLine());
            int k1 = Convert.ToInt32(Console.ReadLine());
            int k2 = Convert.ToInt32(Console.ReadLine());
            int n = Convert.ToInt32(Console.ReadLine());

            int min = n - (a1 * (k1 - 1) + a2 * (k2 - 1));

            int sum = 0;
            int i = 0;
            int j = 0;

            if (a1 * k1 > a2 * k2)
            {
                while (i < a1 && n - sum >= k1)
                {
                    sum += k1;
                    i++;
                }

                while (j < a2 && n - sum >= k2)
                {
                    sum += k2;
                    j++;
                }
            }
            else
            {
                while (i < a2 && n - sum >= k2)
                {
                    sum += k2;
                    i++;
                }

                while (j < a1 && n - sum >= k1)
                {
                    sum += k1;
                    j++;
                }
            }

            int max = i + j;

            Console.WriteLine("{0} {1}", min, max);
        }
    }
}


using System;

namespace OneP
{
     class Program
     {
          static void Main(string[] args)
          {
               int a1,a2,k1,k2,n,n1,n2,t=0;
              a1=Convert.ToInt32(Console.ReadLine());
              a2=Convert.ToInt32(Console.ReadLine());
              k1=Convert.ToInt32(Console.ReadLine());
              k2=Convert.ToInt32(Console.ReadLine());
              n=Convert.ToInt32(Console.ReadLine());

             if(k1<k2)
             {
             n1=k1;k1=k2;k2=n1;
             n2=a1;a1=a2;a2=n2;
             }

              if(a1*(k1-1)+a2*(k2-1)-n<0)
                  n1= n-a1*(k1-1)-a2*(k2-1);
               else n1=0;
             do{
             if (a2!=0){
             n-=k2;
             a2--;
             t++;} else{
                  n-=k1;
                  a1--;
                  t++;
             }}while((n>0)&&(a1>0));

               Console.WriteLine(n1);
               if (n<0)
               Console.WriteLine(t-1);
               else
               Console.WriteLine(t);

           }
      }
}


using System;

class Program
{
    static void Main() {
        int a1 = Int32.Parse(Console.ReadLine());
        int a2 = Int32.Parse(Console.ReadLine());
        int k1 = Int32.Parse(Console.ReadLine());
        int k2 = Int32.Parse(Console.ReadLine());
        int n = Int32.Parse(Console.ReadLine());
        if(k1 > k2)
        {
            int x = k1;
            k1 = k2;
            k2 = x;
            x = a1;
            a1 = a2;
            a2 = x;
        }
        int t1 = Math.Min(n / k1, a1);
        int maxx = t1 + (n - t1 * k1) / k2;
        n -= ((k1 - 1) * a1 + (k2 - 1) * a2);
        int minn = Math.Max(0, n);
        Console.WriteLine(minn + " " + maxx);
    }

}


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

// (づ°ω°)づﾐe★゜・。。・゜゜・。。・゜☆゜・。。・゜゜・。。・゜
public class Solver
{
    public void Solve()
    {
        int n = ReadInt();
        int ans = 0;
        while (n > 0)
        {
            if (n % 8 == 1)
                ans++;
            n /= 8;
        }
        Write(ans);
    }

    #region Main

    protected static TextReader reader;
    protected static TextWriter writer;
    static void Main()
    {
#if DEBUG
        reader = new StreamReader("..\\..\\input.txt");
        //reader = new StreamReader(Console.OpenStandardInput());
        writer = Console.Out;
        //writer = new StreamWriter("..\\..\\output.txt");
#else
        reader = new StreamReader(Console.OpenStandardInput());
        writer = new StreamWriter(Console.OpenStandardOutput());
        //reader = new StreamReader("input.txt");
        //writer = new StreamWriter("output.txt");
#endif
        try
        {
            //var thread = new Thread(new Solver().Solve, 1024 * 1024 * 128);
            //thread.Start();
            //thread.Join();
            new Solver().Solve();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
#if DEBUG
#else
            throw;
#endif
        }
        reader.Close();
        writer.Close();
    }

    #endregion

    #region Read / Write
    private static Queue<string> currentLineTokens = new Queue<string>();
    private static string[] ReadAndSplitLine() { return reader.ReadLine().Split(new[] { ' ', '\t', }, StringSplitOptions.RemoveEmptyEntries); }
    public static string ReadToken() { while (currentLineTokens.Count == 0)currentLineTokens = new Queue<string>(ReadAndSplitLine()); return currentLineTokens.Dequeue(); }
    public static int ReadInt() { return int.Parse(ReadToken()); }
    public static long ReadLong() { return long.Parse(ReadToken()); }
    public static double ReadDouble() { return double.Parse(ReadToken(), CultureInfo.InvariantCulture); }
    public static int[] ReadIntArray() { return ReadAndSplitLine().Select(int.Parse).ToArray(); }
    public static long[] ReadLongArray() { return ReadAndSplitLine().Select(long.Parse).ToArray(); }
    public static double[] ReadDoubleArray() { return ReadAndSplitLine().Select(s => double.Parse(s, CultureInfo.InvariantCulture)).ToArray(); }
    public static int[][] ReadIntMatrix(int numberOfRows) { int[][] matrix = new int[numberOfRows][]; for (int i = 0; i < numberOfRows; i++)matrix[i] = ReadIntArray(); return matrix; }
    public static int[][] ReadAndTransposeIntMatrix(int numberOfRows)
    {
        int[][] matrix = ReadIntMatrix(numberOfRows); int[][] ret = new int[matrix[0].Length][];
        for (int i = 0; i < ret.Length; i++) { ret[i] = new int[numberOfRows]; for (int j = 0; j < numberOfRows; j++)ret[i][j] = matrix[j][i]; } return ret;
    }
    public static string[] ReadLines(int quantity) { string[] lines = new string[quantity]; for (int i = 0; i < quantity; i++)lines[i] = reader.ReadLine().Trim(); return lines; }
    public static void WriteArray<T>(IEnumerable<T> array) { writer.WriteLine(string.Join(" ", array)); }
    public static void Write(params object[] array) { WriteArray(array); }
    public static void WriteLines<T>(IEnumerable<T> array) { foreach (var a in array)writer.WriteLine(a); }
    private class SDictionary<TKey, TValue> : Dictionary<TKey, TValue>
    {
        public new TValue this[TKey key]
        {
            get { return ContainsKey(key) ? base[key] : default(TValue); }
            set { base[key] = value; }
        }
    }
    private static T[] Init<T>(int size) where T : new() { var ret = new T[size]; for (int i = 0; i < size; i++)ret[i] = new T(); return ret; }
    #endregion
}


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

namespace codeforces
{
    class Program
    {
        //16:23
        //reading
        static string ReadLine()
        {
            return Console.ReadLine();
        }
        static string[] ReadArray()
        {
            return ReadLine().Split(' ');
        }
        static List<string> ReadList()
        {
            return ReadArray().ToList();
        }
        static List<int> ReadIntList()
        {
            return ReadArray().Select(c => Convert.ToInt32(c)).ToList();
        }
        static List<long> ReadLongList()
        {
            return ReadArray().Select(c => Convert.ToInt64(c)).ToList();
        }
        static List<ulong> ReadULongList()
        {
            return ReadArray().Select(c => Convert.ToUInt64(c)).ToList();
        }
        static int ToInt(string str)
        {
            return Convert.ToInt32(str);
        }
        static long ToInt64(string str)
        {
            return Convert.ToInt64(str);
        }
        static ulong ToUInt64(string str)
        {
            return Convert.ToUInt64(str);
        }

        static void Main(string[] args)
        {

            Console.WriteLine(1);
            /*
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Console.WriteLine(elapsedMs);*/
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using SB = System.Text.StringBuilder;
//using System.Threading.Tasks;
//using System.Text.RegularExpressions;
//using System.Globalization;
//using System.Diagnostics;
using static System.Console;
using System.Numerics;
using static System.Math;
using pair = Pair<int, int>;

class Program
{
    static void Main()
    {
        //SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
        new Program().solve();
        Out.Flush();
    }
    readonly Scanner cin = new Scanner();
    readonly int[] dd = { 0, 1, 0, -1, 0 }; //→↓←↑
    readonly int mod = 1000000007;
    readonly int dom = 998244353;
    bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
    bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (b.CompareTo(a) < 0) { a = b; return true; } return false; }

    bool calc(int day, int[] K, List<int>[] L, int Sum)
    {
        int used = 0;
        int money = day;
        for (int i = day - 1; i >= 0; i--)
        {
            if (i < L.Length)
            {
                foreach (var v in L[i])
                {
                    int buy = Min(K[v], money);
                    money -= buy;
                    K[v] -= buy;
                    used += buy;
                }
            }
            money = Min(i, money);
        }

        int remain = Sum - used;

        if (day == 7)
        {
            WriteLine(used);
        }
        return remain * 2 <= (day - used);

    }
    void solve()
    {
        int N = cin.nextint;
        int M = cin.nextint;
        var K = cin.scanint;

        var L = new List<int>[N];
        for (int i = 0; i < L.Length; i++)
        {
            L[i] = new List<int>();
        }
        for (int i = 0; i < M; i++)
        {
            L[cin.nextint - 1].Add(cin.nextint - 1);
        }
        int Sum = K.Sum();

        int ng = 0;
        int ok = Sum * 2;
        while (ok - ng > 1)
        {
            int mid = (ok + ng) / 2;

            var Z = new int[N];
            for (int i = 0; i < Z.Length; i++)
            {
                Z[i] = K[i];
            }

            if (calc(mid, Z, L, Sum))
            {
                ok = mid;
            }
            else
            {
                ng = mid;
            }
        }

        WriteLine(ok);

    }

}

static class Ex
{
    public static void join<T>(this IEnumerable<T> values, string sep = " ") => WriteLine(string.Join(sep, values));
    public static string concat<T>(this IEnumerable<T> values) => string.Concat(values);
    public static string reverse(this string s) { var t = s.ToCharArray(); Array.Reverse(t); return t.concat(); }

    public static int lower_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) < 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
    public static int upper_bound<T>(this IList<T> arr, T val) where T : IComparable<T>
    {
        int low = 0, high = arr.Count;
        int mid;
        while (low < high)
        {
            mid = ((high - low) >> 1) + low;
            if (arr[mid].CompareTo(val) <= 0) low = mid + 1;
            else high = mid;
        }
        return low;
    }
}

class Pair<T, U> : IComparable<Pair<T, U>> where T : IComparable<T> where U : IComparable<U>
{
    public T f; public U s;
    public Pair(T f, U s) { this.f = f; this.s = s; }
    public int CompareTo(Pair<T, U> a) => f.CompareTo(a.f) != 0 ? f.CompareTo(a.f) : s.CompareTo(a.s);
    public override string ToString() => $"{f} {s}";
}

class Scanner
{
    string[] s; int i;
    readonly char[] cs = new char[] { ' ' };
    public Scanner() { s = new string[0]; i = 0; }
    public string[] scan => ReadLine().Split();
    public int[] scanint => Array.ConvertAll(scan, int.Parse);
    public long[] scanlong => Array.ConvertAll(scan, long.Parse);
    public double[] scandouble => Array.ConvertAll(scan, double.Parse);
    public string next
    {
        get
        {
            if (i < s.Length) return s[i++];
            string st = ReadLine();
            while (st == "") st = ReadLine();
            s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
            i = 0;
            return next;
        }
    }
    public int nextint => int.Parse(next);
    public long nextlong => long.Parse(next);
    public double nextdouble => double.Parse(next);
}


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

public class Program
{
    int N, M;
    int[] K;
    int[] D, T;

    List<int>[] S;
    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        M = sc.NextInt();

        K = sc.IntArray();
        S = new List<int>[400001];
        for (int i = 0; i <= 400000; i++)
        {
            S[i] = new List<int>();
        }
        for (int i = 0; i < M; i++)
        {
            int d = sc.NextInt();
            int t = sc.NextInt() - 1;
            S[d].Add(t);
        }

        /*
         * 毎日1B獲得
         *
         * n種類
         * 通常2B
         * セール1B
         *
         * タイプiをK_i個
         *
         * m個
         *
         * タイプt_iがd_iにセール
         *
         * 全部買う　最小日数
         */

        /*
         * i日で達成できるか?
         *
         * 最後から見る
         *
         */
        int ok = 400000;
        int ng = 0;
        while (ok - ng > 1)
        {
            int mid = (ok + ng) / 2;
            if (F(mid)) ok = mid;
            else ng = mid;
        }
        Console.WriteLine(ok);

    }

    // t日でできるか?
    bool F(int t)
    {
        int[] tmp = new int[N];
        Array.Copy(K, tmp, N);
        int B = 0;
        for (int i = 1; i <= t; i++)
        {
            B++;
            foreach (var type in S[i])
            {
                int min = Math.Min(tmp[type], B);
                tmp[type] -= min;
                B -= min;
            }
        }
        long sum = 0;
        foreach (var i in tmp)
        {
            sum += i;
        }

        return sum * 2 <= B;
    }

    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();
    }
}