using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Numerics;
public class Codeforces
{
    protected static TextReader reader;
    protected static TextWriter writer;
    static object Solve()
    {
        var n = ReadLong();
        var k = ReadLong();
        var v = 0L;
        var r = 0L;
        for (long i = n; i > 0; i--)
        {
            var kk = 1L;
            r = 0L;
            v = i;
            while (v / kk > 0)
            {
                r += v / kk;
                kk *= k;
            }
            if (r <= n)
            {
                break;
            }
        }
        Write(r == n ? v : v + 1);
        return null;
    }
    static void Main()
    {
        reader = new StreamReader(Console.OpenStandardInput(1024 * 10), System.Text.Encoding.ASCII, false, 1024 * 10);
        writer = new StreamWriter(Console.OpenStandardOutput(1024 * 10), System.Text.Encoding.ASCII, 1024 * 10);
        try
        {
            object result = Solve();
            if (result != null)
                writer.WriteLine(result);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            throw;
        }
        reader.Close();
        writer.Close();
    }
    #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<T>(params T[] array)
    {
        WriteArray(array);
    }

    public static void WriteLines<T>(IEnumerable<T> array)
    {
        foreach (var a in array)
            writer.WriteLine(a);
    }
    static int Next()
    {
        int c;
        int res = 0;
        do
        {
            c = reader.Read();
            if (c == -1)
                return res;
        } while (c != '-' && (c < '0' || c > '9'));
        int sign = 1;
        if (c == '-')
        {
            sign = -1;
            c = reader.Read();
        }
        res = c - '0';
        while (true)
        {
            c = reader.Read();
            if (c < '0' || c > '9')
                return res * sign;
            res *= 10;
            res += c - '0';
        }
    }
    #endregion
}


using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Numerics;
public class Codeforces
{
    protected static TextReader reader;
    protected static TextWriter writer;
    static object Solve()
    {
        var n = ReadLong();
        var k = ReadLong();
        var v = 0L;
        var r = 0L;
        for (long i = n / v + n / (v * v); i > 0; i--)
        {
            var kk = 1L;
            r = 0L;
            v = i;
            while (v / kk > 0)
            {
                r += v / kk;
                kk *= k;
            }
            if (r <= n)
            {
                break;
            }
        }
        Write(r == n ? v : v + 1);
        return null;
    }
    static void Main()
    {
        reader = new StreamReader(Console.OpenStandardInput(1024 * 10), System.Text.Encoding.ASCII, false, 1024 * 10);
        writer = new StreamWriter(Console.OpenStandardOutput(1024 * 10), System.Text.Encoding.ASCII, 1024 * 10);
        try
        {
            object result = Solve();
            if (result != null)
                writer.WriteLine(result);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            throw;
        }
        reader.Close();
        writer.Close();
    }
    #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<T>(params T[] array)
    {
        WriteArray(array);
    }

    public static void WriteLines<T>(IEnumerable<T> array)
    {
        foreach (var a in array)
            writer.WriteLine(a);
    }
    static int Next()
    {
        int c;
        int res = 0;
        do
        {
            c = reader.Read();
            if (c == -1)
                return res;
        } while (c != '-' && (c < '0' || c > '9'));
        int sign = 1;
        if (c == '-')
        {
            sign = -1;
            c = reader.Read();
        }
        res = c - '0';
        while (true)
        {
            c = reader.Read();
            if (c < '0' || c > '9')
                return res * sign;
            res *= 10;
            res += c - '0';
        }
    }
    #endregion
}


using System;
using System.Linq;

namespace NetExperiment
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] nm = Console.ReadLine().Split().Select(s => int.Parse(s)).ToArray();
            int n = nm[0];
            int m = nm[1];
            int[] b = Console.ReadLine().Split().Select(s => int.Parse(s)).ToArray();
            int[] ind = new int[5];
            for(int i = 0; i < m; i++)
            {
                int k = b[i] - 1;
                while(k < n)
                {
                    if (ind[k] == 0)
                        ind[k] = b[i];
                    k++;
                }
            }
            foreach (var i in ind)
                Console.Write(i + " ");
        }
    }
}


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

namespace CrazyComputer
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr1 = new int[2];
            int[] arr2 = new int[arr1[0]];

            arr1 = Array.ConvertAll( Console.ReadLine().Split(' '), int.Parse);
            arr2 = Array.ConvertAll(Console.ReadLine().Split(' '), int.Parse);

            int counter = 0;
            for (int i = 1; i < arr1[0]; i++)
            {

                    if (arr2[i] - arr2[i - 1] <= arr1[1])
                        counter++;
                    else
                        counter = 0;

            }
                   counter++;
            Console.WriteLine(counter);

        }
    }
}


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

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

            int n = 0;
            int k = 0;

            string argLine = Console.ReadLine();
            string[] argString = argLine.Split(' ');

            n = Int32.Parse(argString[0]);
            k = Int32.Parse(argString[1]);

            List<long> numSequence = new List<long>();

            string sequence = "";
            for (int i = 1; i <= n; i++) {
                if (i == 1) {
                    numSequence.Add(1);
                }
                else {
                    numSequence.Add(i);
                    numSequence.AddRange(numSequence.GetRange(0, numSequence.Count - 1));

                }
            }

            Console.WriteLine(numSequence.ElementAt(k-1));

        }
    }
}


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

namespace ExTraining
{
    class Program
    {
        static void Main(string[] args)
        {
            var nums = Console.ReadLine().Split();
            long n = long.Parse(nums[0]);
            long k = long.Parse(nums[1]) - 1;

            List<long> ch = new List<long>();

            List<long> nech = new List<long>();
            nech.Add(1);

            for (long i = 2; i <= n; i++)
            {
                if (i % 2 == 0)
                    ch.Add(i);
                else
                    nech.Add(i);
            }

            if (k >= nech.Count)
                Console.WriteLine(ch[(int)(k - nech.Count)]);
            else
                Console.WriteLine(nech[(int)k]);

        }

    }
}


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

namespace BulkaProga
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] lines = Console.ReadLine().Split(' ');
            long max = long.Parse(lines[0]);
            long index = long.Parse(lines[1]);
            var list1 = new List<long>();
            var list2 = new List<long>();
            var list3 = new List<long>();
            for (int i = 1; i < max + 1; ++i)
            {
                if (i % 2 > 0)
                    list1.Add(i);
                else
                    list2.Add(i);
            }
            for (int i = 0; i < list1.Count; ++i)
                list3.Add(list1[i]);
            for (int i = 0; i < list2.Count; ++i)
                list3.Add(list2[i]);
            Console.WriteLine(list3[(int)index - 1]);
        }
    }
}


using System;
using System.Linq;

namespace CSharp
{
    class _741A
    {
        public static void Main()
        {
            int n = int.Parse(Console.ReadLine());
            var crush = Console.ReadLine().Split().Select(token => int.Parse(token) - 1).ToArray();

            for (int t = 1; t <= 2 * n * n; t++)
            {
                if (Enumerable.Range(0, n).All(i => Enumerable.Repeat(i, 2 * t + 1).Aggregate((a, b) => crush[a]) == i))
                {
                    Console.WriteLine(t);
                    return;
                }
            }

            Console.WriteLine(-1);
        }
    }
}


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

namespace CodeForces_Eevee
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] x = Console.ReadLine().Split('+').Select(int.Parse).ToArray();
            Array.Sort(x);
            for (int i = 0; i < x.Length; i++)
            {
                if (i == x.Length - 1)
                {
                    Console.Write(x[i]);
                    Continue;
                }

                if (x.Length == 1)
                {
                    Console.Write(x[0]);
                }

                else
                {
                    Console.Write(x[i] + "+" );
                }
            }
            Console.ReadKey();
        }
    }
}


string[] y = Console.ReadLine().Split();
		int a = int.Parse(y[0]);
		int b = int.Parse(y[1]);
		int c = int.Parse(y[2]);
		char q='?';
		if(a-b-c>0)
		{
			q = '+';
		}
		else if(b-a-c>0)
		{
			q='-';
		}
		else if(a==b && c==0)
		{
			q='0';
		}
		Console.WriteLine(q);


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

namespace mono
{
    public class Program
    {
        public static char res(int a, int b)
        {
            return a > b ? '+' : (a < b ? '-' : '0');
        }

        public static void Main(string[] args)
        {
            StreamReader sr = new StreamReader("standard.input");
            string s = sr.ReadLine();
            sr.Close();
            string[] arr = s.Split(new char[]{ ' ' });
            char c;
            StreamWriter sw = new StreamWriter("standard.output");
            sw.WriteLine((c = res(int.Parse(arr[0]) + int.Parse(arr[2]), int.Parse(arr[1]))) == res(int.Parse(arr[0]), int.Parse(arr[1]) + int.Parse(arr[2])) ? c : '?');
            sw.Close();
        }
    }
}


#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <utility>
#include <climits>
#include <cstring>
#include <sstream>
#include <cmath>
#include <map>
#include <stack>
#include <sstream>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
typedef long long ll;
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }
template<typename T, typename U> static void amin(T &x, U y) { if(x > y) x = y; }

const int a=1234;
const int b=123456;
const int c=1234567;
int main()
{
    int n,x,y; scanf("%d",&n);
    if(n%a==0){printf("YES\n"); return 0;}
    if(n%b==0){printf("YES\n"); return 0;}
    if(n%c==0){printf("YES\n"); return 0;}
    x=n/c; y=n/b;
    for(int i=0;i<=x;i++)
    {
        if((n-i*a)%b==0){printf("YES\n"); return 0;}
        for(int j=0;j<=y;j++){
            if((n-i*a-j*b)%c==0){printf("YES\n"); return 0;}
        }
    }
    printf("NO\n");
    return 0;
}


using System;

namespace CodeforcesProblemSolving
{
	class Program
	{
		static void Main(string[] args)
		{
			int n = int.Parse(Console.ReadLine());
			bool result = false;

			// a × 1234567 + b × 123456 + c × 1234 = n
			for (int a = 1; a < n; a++)
			{
				for (int b = 1; b < n; b++)
				{
					for (int c = 1; c < n; c++)
					{
						if (a*1234567 + b*123456 + c*1234 == n)
						{
							result = true;
							break;
						}
					}
				}
			}

			Console.WriteLine(result ? "YES" : "NO");
		}
	}
}


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

namespace OmNomCandies
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] s = Console.ReadLine().Split(' ');
            long c = Convert.ToInt64(s[0]);
            long hr = Convert.ToInt64(s[1]);
            long hb = Convert.ToInt64(s[2]);
            long wr = Convert.ToInt64(s[3]);
            long wb = Convert.ToInt64(s[4]);
            long c0 = 0;
            //c0 += ((double)hr / (double)wr) > ((double)hb / (double)wb) ? (c / wr) * hr : (c / wb) * hb;
            //c %= ((double)hr / (double)wr) > ((double)hb / (double)wb) ? wr : wb;
            //int max = c / wr;
            long tempw = 0;
            long temph = 0;
            if ((double)hr / (double)wr > (double)hb / (double)wb)
            {
                for (int i = 0; i < wr; i++)
                {
                    if (i * wb < c)
                    {
                        tempw = c - i * wb;
                        temph = temph < (i * hb + hr * (tempw / wr)) ? (i * hb + hr * (tempw / wr)) : temph;
                    }
                }
            }
            else
            {
                for (int i = 0; i < wb; i++)
                {
                    if (i * wr < c)
                    {
                        tempw = c - i * wr;
                        temph = temph < (i * hr + hb * (tempw / wb)) ? (i * hr + hb * (tempw / wb)) : temph;
                    }
                }
            }
            c0 += temph;
            Console.WriteLine(c0);
        }
    }
}


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

namespace Om_Nom_and_Candies
{
    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)
        {
            long C = Next(), Hr = Next(), Hb = Next(), Wr = Next(), Wb = Next();
            long max = Math.Max((C/Wr)*Hr, (C/Wb)*Hb);

            if (Hr*Wb > Hb*Wr)
            {
                Swap(ref Hr, ref Hb);
                Swap(ref Wr, ref Wb);
            }

            long min = Math.Max(0, C/Wb - Wr);
            for (long i = C/Wb; i >= min; i--)
            {
                long Cr = C - i * Wb;
                if (Cr < 0)
                    continue;
                long count = i * Hb + (Cr / Wr) * Hr;
                if (count > max)
                    max = count;
            }

            writer.WriteLine("{0}", max);
            writer.Flush();
        }

        private static void Swap(ref long a, ref long b)
        {
            long c = a;
            a = b;
            b = c;
        }

        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;
using System.Threading.Tasks;

namespace CodeForces
{
    class Program
    {
        static void Main(string[] args)
        {
            string input = Console.ReadLine();
            string[] values = input.Split(' ');
            double n = Convert.ToDouble(values[0]);
            double m = Convert.ToDouble(values[1]);
            double a = Convert.ToDouble(values[2]);
            double b = Convert.ToDouble(values[3]);

            int burles = 0;

            if (n / m % 1 > 0
                || n / m % 1 < 0)
            {
                double diff = n % m;

                double burlesDemolish = diff * b;
                double burlesBuild = (m - diff) * a;

                if (burlesDemolish > burlesBuild)
                {
                    burles = Convert.ToInt32(burlesBuild);
                }
                else if (burlesDemolish < burlesBuild)
                {
                    burles = Convert.ToInt32(burlesDemolish);
                }
                else
                {
                    burles = Convert.ToInt32(burlesBuild);
                }
            }

            Console.WriteLine(burles);
        }
    }
}


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

public class Program
{
	public static void Main()
	{
		var line=Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
		long n=line[0], m=line[1], a=line[2], b=line[3];
		long result=0;

		if(n%m==0)
			result=0;
		else
		{
				long demolish=(n%m)*b;
				int i=1;
				while((n+i)%m!=0)
				{
					i++;
				}
				long build=i*a;
				result=Math.Min(demolish,build);
		}
		Console.WriteLine(result);
	}
}


using System;

namespace bla
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			string[] s = Console.ReadLine ().Split ();
			int n = int.Parse (s [0]);
			int m = int.Parse (s [1]);
			int a = int.Parse (s [2]);
			int b = int.Parse (s [3]);
			int fd = (n%m) * b;
			int fb = (m - (n % m)) * a;
			Console.WriteLine (Math.Min(fd, fb));
		}
	}
}


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

namespace ConsoleApplication6
{
    public static class CF186_1
    {
        public static string GetBestPrice(string d)
        {
            long res = Convert.ToInt64(d);
            if (res<0)
            {
                string str = d.ToString();
                long dn = Convert.ToInt64(str.Substring(0, str.Length - 1));

                string lastCh = str.Substring(str.Length - 1, 1);
                long dn1 = Convert.ToInt64(str.Substring(0, str.Length - 2)+lastCh);
                res = dn > dn1 ? dn : dn1;
            }
            return res.ToString();
        }
    }
}


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

namespace FootballChampionship
{
    internal class Team
    {
        public String Name
        {
            get;
            set;
        }

        public int GamesPlayed
        {
            get;
            set;
        }

        public int Points
        {
            get;
            set;
        }

        public int Scored
        {
            get;
            set;
        }

        public int Missed
        {
            get;
            set;
        }
    }

    internal class Program
    {
        private static readonly Dictionary<string, Team> tournament = new Dictionary<string, Team>();

        private static void Main(string[] args)
        {
            Process(Console.ReadLine());
            Process(Console.ReadLine());
            Process(Console.ReadLine());
            Process(Console.ReadLine());
            Process(Console.ReadLine());

            String result = Solve();
            Console.Out.WriteLine(result);
        }

        private static string Solve()
        {
            var teams = new List<Team>(tournament.Values);
            Team o = teams.Where(t => t.GamesPlayed == 2 && t.Name != "BERLAND").First();
            Team b = teams[2].Name == "BERLAND" ? teams[2] : teams[3];
            int bScored = b.Scored;
            int oScored = o.Scored;
            int bMissed = b.Missed;
            int oMissed = o.Missed;

            b.Points += 3;
            for (int i = 1; i < 60; i++)
            {
                for(int j = 0; j < 60; j++)
                {
                    b.Scored = bScored;
                    o.Scored = oScored;
                    b.Missed = bMissed;
                    o.Missed = oMissed;

                    b.Scored += (i + j);
                    b.Missed += j;
                    o.Scored += j;
                    o.Missed += (i + j);
                    SortTeams(teams);
                    if (teams[0].Name == "BERLAND" || teams[1].Name == "BERLAND")
                    {
                        return String.Format("{0}:{1}", i + j, j);
                    }
                }
            }
            return "IMPOSSIBLE";
        }

        private static void SortTeams(List<Team> teams)
        {
            teams.Sort(
                (t1, t2) =>
                {
                    int x = t2.Points.CompareTo(t1.Points);
                    if (x != 0)
                    {
                        return x;
                    }
                    x = (t2.Scored - t2.Missed).CompareTo(t1.Scored - t1.Missed);
                    if (x != 0)
                    {
                        return x;
                    }
                    x = t2.Scored.CompareTo(t1.Scored);
                    if (x != 0)
                    {
                        return x;
                    }
                    return t1.Name.CompareTo(t2.Name);
                });
        }

        private static void Process(string line)
        {
            String[] parts = line.Split(' ');
            String team1 = parts[0];
            String team2 = parts[1];
            int[] scores = parts[2].Split(':').Select(x => Convert.ToInt32(x)).ToArray();
            int score1 = scores[0];
            int score2 = scores[1];

            AddTeam(team1, score1, score2);
            AddTeam(team2, score2, score1);
        }

        private static void AddTeam(string team, int scored, int missed)
        {
            if (!tournament.ContainsKey(team))
            {
                tournament[team] = new Team { Name = team };
            }
            tournament[team].GamesPlayed++;
            tournament[team].Scored += scored;
            tournament[team].Missed += missed;
            tournament[team].Points += scored > missed ? 3 : (scored == missed ? 1 : 0);
        }
    }
}


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

namespace Task7_NEW
{
    class Task7_NEW
    {
        static void Main(string[] args)
        {
           // Console.WriteLine("Enter an area of floor. Enter a number that is in the range" +
           //     "1<=floor<=10000");
            //parameter A
            string floorAreaString = Console.ReadLine();
            int floorArea = Convert.ToInt32(floorAreaString);

        //    Console.WriteLine("Enter area of wall B.");
            //parameter B
            string wallAreaBString = Console.ReadLine();
            int wallAreaB = Convert.ToInt32(wallAreaBString);

           //Console.WriteLine("Enter area of wall C.");
            //parameter C
            string wallAreaCString = Console.ReadLine();
            int wallAreaC = Convert.ToInt32(wallAreaCString);
            int result = SolveTask(floorArea, wallAreaB, wallAreaC);

            Console.WriteLine(result);
          //  Console.ReadKey();
        }

        private static int SolveTask(int floorArea, int wallAreaB, int wallAreaC)
        {
            double width = Math.Sqrt(floorArea * wallAreaC / wallAreaB);
            double length = floorArea / width;
            double height = wallAreaB / length;
            int amount;
            return amount = Convert.ToInt32(width + length + height);
        }
    }
}


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

namespace ConsoleApplication1.Combynatorycs
{
    class _610
    {
        public static long nCr(int n, int r)
        {
            // naive: return Factorial(n) / Factorial(r) * Factorial(n - r);
            return nPr(n, r) / Factorial(r);
        }
        public static long nPr(int n, int r)
        {
            // naive: return Factorial(n) / Factorial(n - r);
            return FactorialDivision(n, n - r);
        }

        private static long FactorialDivision(int topFactorial, int divisorFactorial)
        {
            long result = 1;
            for (int i = topFactorial; i > divisorFactorial; i--)
                result *= i;
            return result;
        }

        private static long Factorial(int i)
        {
            if (i <= 1)
                return 1;
            return i * Factorial(i - 1);
        }
        static void Main(String[] args)
        {
            var n = int.Parse(Console.ReadLine());
            int d = (int)Math.Pow(10, 9) + 7;
            var  result = 1;
           for(var i = 1; i <= 3 * n; i++)
            {
                result *= 3;
                result = result % d;
            }
            var m = 1;
           for(var i = 0; i < n; i++)
            {
                m *= 7;
                m = m % d;
            }

            Console.WriteLine(Math.Abs(result - m));
        }
    }
}


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

class PairVariable : IComparable<PairVariable>
{
    public int a, b;
    public int c;
    public PairVariable()
    {
        a = b = 0;
        c = 0;
    }
    public PairVariable(string s)
    {
        string[] q = s.Split();
        a = int.Parse(q[0]);
        b = int.Parse(q[1]);
        c = 0;

    }

    public PairVariable(int a, int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;

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

    public int CompareTo(PairVariable other)
    {
        return this.a.CompareTo(other.a);
    }
}
namespace ConsoleApplication1
{
    class Program
    {

        static char[,] s = new char[10, 10];

        static List<PairVariable> position = new List<PairVariable>();

        static void Main(string[] args)
        {
			int[,]mark=new int[10,10];
            bool f = true;
            for (int i = 1; i <= 8; i++)
            {
               string q=Console.ReadLine();
                for (int e = 1; e <= 8; e++)
                {
                    s[i, e] = q[e - 1];
                }
            }

                for (int i = 1; i <= 8; i++)
                {
                    for (int e = 1; e <= 8; e++)
                    {
                        if (s[i, e] == 'S')
                        {
                            s[i, e] = '.';
                            position.Add(new PairVariable(i, e));
                        }
                    }
                }
                int x = 8;
                int y = 1;
                List<PairVariable> d = new List<PairVariable>();
                d.Add(new PairVariable(x, y));
                for (int i = 0; i < 8; i++)
                {
                    if (d.Count == 0)
                    {
                        Console.WriteLine("LOSE");
                        return;
                    }
                    int kk = d.Count;
                    for (int e = 0; e < kk; e++)
                    {
                        int xx = d[e].a;
                        int yy = d[e].b;
                        int x1 = xx - 1;
                        int y1 = yy;
                        bool q = true;
                        for (int j = 0; j < position.Count; j++)
                        {
                            if (position[j].a == x1 && position[j].b == y1)
                            {
                                q = false;
                            }
                        }
                        if (s[x1, y1] != '.')
                        {
                            q = false;
                        }
						if(mark[x1,y1]==1)
						{
							q=false;
						}
                        if (q)
                        {
							mark[x1,y1]=1;
                            d.Add(new PairVariable(x1, y1));
                        }
                        x1 = xx - 1;
                        y1 = yy - 1;
                        q = true;
                        for (int j = 0; j < position.Count; j++)
                        {
                            if (position[j].a == x1 && position[j].b == y1)
                            {
                                q = false;
                            }
                        }
                        if (s[x1, y1] != '.')
                        {
                            q = false;
                        }
						if(mark[x1,y1]==1)
						{
							q=false;
						}
                        if (q)
                        {
							mark[x1,y1]=1;
                            d.Add(new PairVariable(x1, y1));
                        }
                        x1 = xx - 1;
                        y1 = yy + 1;

                        q = true;
                        for (int j = 0; j < position.Count; j++)
                        {
                            if (position[j].a == x1 && position[j].b == y1)
                            {
                                q = false;
                            }
                        }

                        if (s[x1, y1] != '.')
                        {
                            q = false;
                        }

						if(mark[x1,y1]==1)
						{
							q=false;
						}
                        if (q)
                        {
							mark[x1,y1]=1;
                            d.Add(new PairVariable(x1, y1));
                        }
                        x1 = xx;
                        y1 = yy + 1;
                        q = true;
                        for (int j = 0; j < position.Count; j++)
                        {
                            if (position[j].a == x1 && position[j].b == y1)
                            {
                                q = false;
                            }
                        }
                        if (s[x1, y1] != '.')
                        {
                            q = false;
                        }
						if(mark[x1,y1]==1)
						{
							q=false;
						}
                        if (q)
                        {
							mark[x1,y1]=1;
                            d.Add(new PairVariable(x1, y1));
                        }
                        x1 = xx;
                        y1 = yy - 1;
                        q = true;
                        for (int j = 0; j < position.Count; j++)
                        {
                            if (position[j].a == x1 && position[j].b == y1)
                            {
                                q = false;
                            }
                        }
                        if (s[x1, y1] != '.')
                        {
                            q = false;
                        }
						if(mark[x1,y1]==1)
						{
							q=false;
						}
                        if (q)
                        {
							mark[x1,y1]=1;
                            d.Add(new PairVariable(x1, y1));
                        }
                        x1 = xx+1;
                        y1 = yy - 1;
                        q = true;
                        for (int j = 0; j < position.Count; j++)
                        {
                            if (position[j].a == x1 && position[j].b == y1)
                            {
                                q = false;
                            }
                        }
                        if (s[x1, y1] != '.')
                        {
                            q = false;
                        }
						if(mark[x1,y1]==1)
						{
							q=false;
						}
                        if (q)
                        {
							mark[x1,y1]=1;
                            d.Add(new PairVariable(x1, y1));
                        }
                        x1 = xx+1;
                        y1 = yy + 1;
                        q = true;
                        for (int j = 0; j < position.Count; j++)
                        {
                            if (position[j].a == x1 && position[j].b == y1)
                            {
                                q = false;
                            }
                        }
                        if (s[x1, y1] != '.')
                        {
                            q = false;
                        }
						if(mark[x1,y1]==1)
						{
							q=false;
						}
                        if (q)
                        {
							mark[x1,y1]=1;
                            d.Add(new PairVariable(x1, y1));
                        }
                        x1 = xx+1;
                        y1 = yy;
                        q = true;
                        for (int j = 0; j < position.Count; j++)
                        {
                            if (position[j].a == x1 && position[j].b == y1)
                            {
                                q = false;
                            }
                        }
                        if (s[x1, y1] != '.')
                        {
                            q = false;
                        }
						if(mark[x1,y1]==1)
						{
							q=false;
						}
                        if (q)
                        {
							mark[x1,y1]=1;
                            d.Add(new PairVariable(x1, y1));
                        }
                    }
                    for (int e = 0; e < position.Count; e++)
                    {
                        position[e].a++;
                    }

                    for (int e = 0; e < position.Count; e++)
                    {
                        for (int j = 0; j < d.Count; j++)
                        {
                            if (position[e].a == d[j].a && position[e].b == d[j].b)
                            {
                                d.RemoveAt(j);
                            }

                        }
                    }

                }
                Console.WriteLine("WIN");

        }
    }
}/**/


using System;
using System.Linq;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine(), s1 = s, s2 = "", s3 = s, s4 = "";
            for(int i = s.Length - 1; i >= 0; i--)
                s2 += s[i];
            s4 = s2;
            while (s1.Equals(s2) && s3.Equals(s4) && s1.Length > 0)
            {
                s1 = s1.Substring(0, s1.Length - 1);
                s2 = s2.Substring(1, s2.Length - 1);
                s3 = s3.Substring(1, s3.Length - 1);
                s4 = s4.Substring(0, s4.Length - 1);
            }
            Console.WriteLine(s1.Length);
            Console.WriteLine(s2);

        }
    }
}


using System;

namespace CF_981_A
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = Console.ReadLine();

            for (int i = 0; i < str.Length-2; i++)
            {
                for (int j = str.Length-i; j > 1; j--)
                {
                    if (!IsPalindrom(str.Substring(i, j)))
                    {
                        Console.WriteLine(j);
                        return;
                    }
                }
            }
            Console.WriteLine(0);
        }

        private static bool IsPalindrom(string v)
        {
            int i = 0;
            int j = v.Length - 1;
            while (i<j)
            {
                if (v[i] != v[j]) return false;
                i++; j--;
            }
            return true;
        }
    }
}


static void Task110A()
{
    var number = Console.ReadLine();
    var arr = number.ToArray();
    bool flag = true;
    for (int i = 0; i < number.Length - 1; ++i)
    {
        if ((number[i] == '7' || number[i] == '4') && ((number[i + 1] == '7' || number[i + 1] == '4')))
        {
            flag = true;
        }
        else
        {
            flag = false;
        }

    }
    if (flag)
    {
        Console.WriteLine("YES");
    }
    else
    {
        Console.WriteLine("NO");

    }


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

namespace Ability_To_Convert
{
    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 = int.Parse(reader.ReadLine());
            string s = reader.ReadLine();

            long prev = 0;
            long ans = 0;
            long pow = 1;
            long bpow = 1;
            int lastback0 = s.Length-1;
            for (int i = s.Length - 1; i >= 0; i--)
            {
                long next = prev + pow*(s[i] - '0');
                if (next >= n)
                {
                    ans = ans + bpow*prev;
                    bpow *= n;
                    pow = 10;
                    while (i + 1 < lastback0 && s[i + 1] == '0')
                    {
                        i++;
                    }
                    lastback0 = Math.Min(i, lastback0);
                    prev = s[i] - '0';
                }
                else
                {
                    prev = next;
                    pow *= 10;
                }
            }
            ans = ans + bpow*prev;

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


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

namespace A
{
    class Program
    {
        static int rec(int n, int k)
        {
            if (n==1 && (k==3 || k==4 || k==5))
            {
                return 0;
            }
            if (n==1 && k==2)
            {
                return 1;
            }
            if (n==1)
            {
                return 1000000;
            }
            int min = int.MaxValue;
            for (int i = 2; i <= 5; i++)
            {
                min = Math.Min(min, rec(n - 1, k - i));
                if (i==2)
                {
                    min++;
                }
            }
            return min;
        }
        static void Main(string[] args)
        {
            string[] input = Console.ReadLine().Split(' ');
            int n = int.Parse(input[0]);
            int k = int.Parse(input[1]);
            Console.WriteLine(rec(n, k));
            Console.ReadLine();
        }
    }
}


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, s;
            int tempsum;

            int diff;
            int[] marks = new int[n];
            int i;
            for (i = 0; i < n; i++)
                marks[i] = 2;
            int cnt;

            while (true)
            {
                tempsum = marks.Sum();
                diff = s - tempsum;
                if (diff == 0)
                    break;

                if (diff > n)
                    cnt = n;
                else
                    cnt = diff;
                for (i = 0; i < diff; i++)
                    marks[i] += 1;
            }

            cnt = marks.Where(item => item == 2).Count();
            System.Console.WriteLine(cnt.ToString());
            System.Console.ReadKey();

        }
    }
}


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n, s;
            int tempsum;
            string ptr;
            ptr = System.Console.ReadLine().Trim();
            string[] sarr = ptr.Split(' ');
            n = Int32.Parse(sarr[0].Trim());
            s = Int32.Parse(sarr[1].Trim());
            int diff;
            int[] marks = new int[n];
            int i;
            for (i = 0; i < n; i++)
                marks[i] = 2;
            int cnt;

            while (true)
            {
                tempsum = marks.Sum();
                diff = s - tempsum;
                if (diff == 0)
                    break;

                if (diff > n)
                    cnt = n;
                else
                    cnt = diff;
                for (i = 0; i < diff; i++)
                    marks[i] += 1;
            }

            cnt = marks.Where(item => item == 2).Count();
            System.Console.WriteLine(cnt.ToString());

        }
    }
}


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

namespace ConsoleApplication1
{
    class Program
    {
        static int nextInt()
        {
            int t = Console.Read();
            while ((t < '0' || t > '9') && t != '-') t = Console.Read();
            int sign = 1;
            if (t == '-')
            {
                sign = -1;
                t = Console.Read();
            }
            int x = 0;
            while (t >= '0' && t <= '9')
            {
                x *= 10;
                x += t - '0';
                t = Console.Read();
            }
            return x * sign;
        }
        static void Main(string[] args)
        {
            int n = nextInt();
            int k = nextInt();
            if (n * 3 <= k)
                Console.WriteLine(0);
            else
                Console.WriteLine(n * 3 - k);
            n = nextInt();
        }
    }
}


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

namespace CodeForces {
    struct Node {
        public int X, Y, D;
        public Node(int x,int y, int d) {
            X = x;
            Y = y;
            D = d;
        }
    }

    class Pair : IComparable<Pair> {
        public int First, Second;
        public Pair(int f, int s) {
            First = f;
            Second = s;
        }

        public int CompareTo(Pair other) {
            return other.Second.CompareTo(Second);
        }
    }

    class Desc : IComparer<int> {
        public int Compare(int x, int y) {
            return y.CompareTo(x);
        }
    }

    class Cf {
        static TextReader input;
        static void Main(string[] args) {
#if TESTS
            Stopwatch sw = new Stopwatch();
            sw.Start();
            input = new StreamReader("input.txt");
#else
            input = Console.In;
#endif
            Solve();
#if TESTS
            Console.WriteLine();
            Console.WriteLine("Milliseconds: {0}", sw.ElapsedMilliseconds);
            Console.ReadLine();
#endif
        }

        static int n, m, k, ans = int.MaxValue;
        static Graph g;
        static void Solve() {
            n = nextInt();
            k = nextInt();
            go(k, 0, 0);
            Console.WriteLine(ans);
        }

        static void go(int num, int cnt, int pos) {
            if (num < 0)
                return;
            if (num == 0 && pos == n)
                ans = Math.Min(ans, cnt);
            for (int i = 2; i < 6; i++) {
                int t = i == 2 ? 1 : 0;
                go(num - i, cnt + t, pos + 1);
            }
        }

        static int SquaredDistanceBetweenSegments(int x1, int y1, int x2, int y2, int xx1, int yy1, int xx2, int yy2) {
            return Math.Min(Math.Min(SquaredDistanceToSegment(x1, y1, xx1, yy1, xx2, yy2), SquaredDistanceToSegment(x2, y2, xx1, yy1, xx2, yy2)),
                Math.Min(SquaredDistanceToSegment(xx1, yy1, x1, y1, x2, y2), SquaredDistanceToSegment(xx2, yy2, x1, y1, x2, y2)));
        }

        static int SquaredDistanceToSegment(int x, int y, int x1, int y1, int x2, int y2) {
            int result = Math.Min(SquaredDistance(x, y, x1, y1), SquaredDistance(x, y, x2, y2));
            if (x1 == x2 && y1 <= y && y <= y2)
                result = (x - x1) * (x - x1);
            else if (y1 == y2 && x1 <= x && x <= x2)
                result = (y - y1) * (y - y1);
            return result;
        }

        static int SquaredDistance(int ax, int ay, int bx, int by) {
            return (ax - bx) * (ax - bx) + (ay - by) * (ay - by);
        }

        #region read helpers
        static int nextInt() {
            int t = input.Read();
            while (t < '0' || t > '9') t = input.Read();
            int x = 0;
            while (t >= '0' && t <= '9') {
                x *= 10;
                x += t - '0';
                t = input.Read();
            }
            return x;
        }

        static long nextLong() {
            int t = input.Read();
            while (t < '0' || t > '9') t = input.Read();
            long x = 0;
            while (t >= '0' && t <= '9') {
                x *= 10;
                x += t - '0';
                t = input.Read();
            }
            return x;
        }

        public static List<int> ReadIntList() {
            return input.ReadLine().Split(' ').Select(x => int.Parse(x)).ToList();
        }

        public static int[] ReadIntArray() {
            return input.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
        }

        public static long[] ReadLongArray() {
            return input.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
        }

        public static List<long> ReadLongList() {
            return input.ReadLine().Split(' ').Select(x => long.Parse(x)).ToList();
        }

        public static string ReadLine() {
            return input.ReadLine();
        }

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

        public static long ReadLong() {
            return long.Parse(input.ReadLine());
        }
#endregion
    }

    public class SegmentTree {
        private int[] a, t;
        private int n;
        public SegmentTree(int[] a) {
            n = a.Length;
            this.a = a;
            t = new int[n << 2];
            buildSegmentTree(0, n - 1, 1);
        }

        private void buildSegmentTree(int l, int r, int v) {
            if (l == r)
                t[v] = a[l];
            else {
                int m = (l + r) >> 1;
                int vl = v << 1;
                int vr = (v << 1) | 1;
                buildSegmentTree(l, m, vl);
                buildSegmentTree(m + 1, r, vr);
                t[v] = t[vl] + t[vr];
            }
        }

        public int QuerySegmentTree(int l, int r) {
            return querySegmentTree(1, l, r, 0, n - 1);
        }

        public void Update(int v, int tl, int tr, int pos, int new_val) {
            if (tl == tr)
                t[v] = new_val;
            else {
                int tm = (tl + tr) / 2;
                if (pos <= tm)
                    Update(v << 1, tl, tm, pos, new_val);
                else
                    Update(v << 1 + 1, tm + 1, tr, pos, new_val);
                t[v] = t[v << 1] + t[v << 1 + 1];
            }
        }

        private int querySegmentTree(int v, int l, int r, int tl, int tr) {
            if (l > r)
                return 0;
            if (l == tl && r == tr)
                return t[v];
            int m = (tl + tr) >> 1;
            return querySegmentTree(v << 1, l, Math.Min(r, m), tl, m) + querySegmentTree((v << 1) | 1, Math.Max(l, m + 1), r, m + 1, tr);
        }
    }
    class Graph : List<List<int>> {
        public Graph(int num): base(num) {
            for (int i = 0; i < num; i++) {
                this.Add(new List<int>());
            }
        }
    }

    class Heap<T> : IEnumerable<T> where T : IComparable<T> {
        private List<T> heap = new List<T>();
        private int heapSize;
        private int Parent(int index) {
            return (index - 1) >> 1;
        }
        public int Count {
            get { return heap.Count; }
        }
        private int Left(int index) {
            return (index << 1) | 1;
        }
        private int Right(int index) {
            return (index << 1) + 2;
        }
        private void Max_Heapify(int i) {
            int l = Left(i);
            int r = Right(i);
            int largest = i;
            if (l < heapSize && heap[l].CompareTo(heap[i]) > 0)
                largest = l;
            if (r < heapSize && heap[r].CompareTo(heap[largest]) > 0)
                largest = r;
            if (largest != i) {
                T temp = heap[largest];
                heap[largest] = heap[i];
                heap[i] = temp;
                Max_Heapify(largest);
            }
        }
        private void BuildMaxHeap() {
            for (int i = heap.Count >> 1; i >= 0; --i)
                Max_Heapify(i);
        }

        public IEnumerator<T> GetEnumerator() {
            return heap.GetEnumerator();
        }

        public void Sort() {
            for (int i = heap.Count - 1; i > 0; --i) {
                T temp = heap[i];
                heap[i] = heap[0];
                heap[0] = temp;
                --heapSize;
                Max_Heapify(0);
            }
        }

        public T Heap_Extract_Max() {
            T max = heap[0];
            heap[0] = heap[--heapSize];
            Max_Heapify(0);
            return max;
        }

        public void Clear() {
            heap.Clear();
            heapSize = 0;
        }

        public void Insert(T item) {
            if (heapSize < heap.Count)
                heap[heapSize] = item;
            else
                heap.Add(item);
            int i = heapSize;
            while (i > 0 && heap[Parent(i)].CompareTo(heap[i]) < 0) {
                T temp = heap[i];
                heap[i] = heap[Parent(i)];
                heap[Parent(i)] = temp;
                i = Parent(i);
            }
            ++heapSize;
        }

        IEnumerator IEnumerable.GetEnumerator() {
            return ((IEnumerable)heap).GetEnumerator();
        }
    }
    public class Treap {
        private static Random rand = new Random();
        public static Treap Merge(Treap l, Treap r) {
            if (l == null) return r;
            if (r == null) return l;
            Treap res;
            if (l.y > r.y) {
                Treap newR = Merge(l.right, r);
                res = new Treap(l.x, l.y, l.left, newR);
            }
            else {
                Treap newL = Merge(l, r.left);
                res = new Treap(r.x, r.y, newL, r.right);
            }
            return res;
        }

        public void Split(int x, out Treap l, out Treap r) {
            Treap newTree = null;
            if (this.x <= x) {
                if (right == null)
                    r = null;
                else
                    right.Split(x, out newTree, out r);
                l = new Treap(this.x, y, left, newTree);
            }
            else {
                if (left == null)
                    l = null;
                else
                    left.Split(x, out l, out newTree);
                r = new Treap(this.x, y, newTree, right);
            }
        }

        public Treap Add(int x) {
            Treap l, r;
            Split(x, out l, out r);
            Treap m = new Treap(x, rand.Next());
            return Merge(Merge(l, m), r);
        }

        public Treap Remove(int x) {
            Treap l, m, r;
            Split(x - 1, out l, out r);
            r.Split(x, out m, out r);
            return Merge(l, r);
        }

        private int x, y, cost, maxTreeCost;
        private Treap left, right;
        public Treap(int x, int y, Treap l, Treap r) {
            this.x = x;
            this.y = y;
            left = l;
            right = r;
        }
        public Treap(int x, int y) : this(x, y, null, null) { }
        public void InOrder() {
            inOrder(this);
        }
        private void inOrder(Treap t) {
            if (t == null) return;
            inOrder(t.left);
            Console.WriteLine(t.x);
            inOrder(t.right);
        }
    }
    public static class Extensions {
        public static void Fill(this int[] array, int val) {
            for (int i = 0; i < array.Length; i++) {
                array[i] = val;
            }
        }

        public static void Fill(this double[] array, double val) {
            for (int i = 0; i < array.Length; i++) {
                array[i] = val;
            }
        }

        public static int ToInt(this string s) {
            return int.Parse(s);
        }

        public static string Fill(this char c, int count) {
            char[] r = new char[count];
            for (int i = 0; i < count; ++i)
                r[i] = c;
            return new string(r);
        }

        public static void Print<T>(this IEnumerable<T> arr) {
            foreach (T t in arr)
                Console.Write("{0} ", t);
        }
    }
}


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] str = Console.ReadLine().Split(' ');
            int n = int.Parse(str[0]);
            int m = int.Parse(str[1]);

            ArrayList A = new ArrayList();
            for (int i = 1; i <= n; i++)
            {
                A.Add(1);
            }
            int current = (int)A[0];
            int c = 1;
            int x = 1;
            int count = 1;
            while (c < c + 1) {
                if (x == m) { A.Add(1); x = 1; } else { x++; }
                if (A.Count<= c) { break; }
                count++;
                c++;
            }
            Console.WriteLine(count);
            Main(new string[0]) ;
        }
    }
}


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

namespace ConsoleApplication1
{
    class Program
    {
        private static void MyMain()
        {
            int initial;
            int buyDay;
            RInts(out initial, out buyDay);

            int worn = initial;
            int div = initial/buyDay;
            int rem = initial % buyDay;
            while (!(rem == 0 && div ==1)) {
                worn += div;
                var newDiv = (div + rem) / buyDay;
                rem = (div + rem) % buyDay;
                div = newDiv;
            }
            worn = 1 + worn;
            WLine(worn);

        }

        static void Main()
        {
#if !HOME
            MyMain();
#else
            var tmp = Console.In;
            Console.SetIn(new StreamReader("in.txt"));
            while (Console.In.Peek() > 0)
            {
                MyMain();
            }
            Console.In.Close();
            Console.SetIn(tmp);
            Console.ReadKey();
#endif
        }

        #region Utils
        static string RLine()
        {
            return Console.ReadLine();
        }

        static int RInt()
        {
            var str = Console.ReadLine();
            return Convert.ToInt32(str);
        }

        static int[] RInts()
        {
            return Array.ConvertAll(Console.ReadLine().Split(' '), s => Convert.ToInt32(s));
        }

        static void RInts(out int a, out int b)
        {
            var ints = RInts();
            a = ints[0];
            b = ints[1];
        }

        static void WLine<T>(T s)
        {
            Console.WriteLine(s);
        }

        static void WYES()
        {
            WLine("YES");
        }

        static void WNO()
        {
            WLine("NO");
        }
        #endregion
    }
}


using System;

namespace CodeforcesTasks
{
    internal class Program
    {
        public static void Main(string[] args)
        {
            var number = Convert.ToInt32(Console.ReadLine(), 2);
            var n = 1;
            for (var i = 0;; i++)
            {
                if (number <= n)
                {
                    Console.WriteLine(i);
                    return;
                }

                n *= 4;
            }
        }

    }
}


using System;

namespace codeforceCSharp
{
    class Program
    {
        static int binToDec(int bin)
        {
            int dec = 0;
            int n = 0;
            while (bin != 0)
            {
                if (bin % 2 == 1)
                    dec += (int)Math.Pow(2, n);
                bin /= 10;
                n++;
            }
            return dec;
        }
        static void Main(string[] args)
        {

            int bin, dec;

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

            dec = binToDec(bin);

            int n = 0;
            while (Math.Pow(4, n) < dec)
            {
                n++;
            }
            Console.WriteLine(n);
            Console.ReadLine();
        }
    }
}


namespace _17A
{
    class Program
    {
        static void Main(string[] args)
        {
            string line = Console.ReadLine();
            string[] parts = line.Split(new char[] { ' ' });

            int n = int.Parse(parts[0]);
            int k = int.Parse(parts[1]);

            int count = 0;

            while (n >=1)
            {
                int temp0 = previousPrime(n);
                int temp1 = temp0;

                while (temp1 >= 1)
                {
                    int temp2 = previousPrime(temp1);
                    int temp3 = previousPrime(temp2);

                    if (temp2 + temp3 + 1 == temp0)
                    {
                        count++;
                        break;
                    }
                    temp1 = temp2;

                }

                n = temp0;
            }

            if (count >= k)
            {
                Console.WriteLine("YES");
            }
            else
                Console.WriteLine("NO");

            Console.ReadKey();
        }

        static bool isPrime(int n)
        {
            if (n <= 2)
            {
                return true;
            }
            else
            {
                int i = 2;

                while (i < n)
                {
                    if (n % i == 0)
                        return false;
                    i++;
                }
                return true;
            }

        }

        static int previousPrime(int p)
        {
            int temp = p - 1;
            while (true)
            {
                if (isPrime(temp))
                    return temp;

                temp--;
            }
        }
    }
}


        public static string GetBestSolution(int n, string str)
        {
            if (n < 26)
            {
                return "NO";
            }
            char[] letters = new char[n];

            letters = str.ToLower().ToCharArray();
            List<char> charCount = new List<char>();
            for (int i = 0; i < n; i++)
            {
                if (!charCount.Contains(letters[i]))
                {
                    charCount.Add(letters[i]);
                }

            }

            return charCount.Count == 26 ? "YES" : "NO";
        }


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

namespace C_Anton_and_tale
{
    class Program
    {
        static void Main(string[] args)
        {
            long[] input = Console.ReadLine().Split(' ').Select(x => long.Parse(x)).ToArray();
            //long t1 = input[0] * 2;
            long z = input[0];
            long d = input[1];
            long lim = input[0];
            long step = 0;
            do
            {
                step++;
                z += d; z = Math.Min(z, lim);
                z -= step;

            } while (z > 0);
            Console.WriteLine(step);

        }
    }
}


using System;
using System.Collections.Generic;

public class Program
{
    public static HashSet<char> vowels = new HashSet<char>(new string[] { 'A', 'E', 'I', 'O', 'U', 'Y' });

    public static void Main(string[] args)
    {
        var input = Console.ReadLine().ToUpper();

        var max = int.MinValue;
        var position = 0;

        while (position != input.Length)
        {
            var tuple = NextVowel(input, position);
            if (tuple.Item1 == -1)
            {
                break;
            }
            position = tuple.Item1;
            max = Math.Max(tuple.Item2, max);
        }

        Console.WriteLine(max);
    }

    public static Tuple<int, int> NextVowel(string input, int position)
    {
        for (var i = position; i < input.Length; i++)
        {
            if (vowels.Contains(input[i]))
            {
                return new Tuple<int, int>(i, (i + 1) - position);
            }
        }

        return new Tuple<int, int>(-1, -1);
    }
}


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

public static partial class MyExtension
{
    public class Solution
    {
        /* Head ends here */

        public const int PAD_FACTOR = 7;

        public static int greedy(int a, int b, int f, int k)
        {
            var ans = 0;
            var stock = b;
            var spbu = f;
            while (k-- > 0)
            {
                var canSpbu = stock >= spbu;
                if (!canSpbu)
                    return -1;
                // decide whether to refill or not
                var canStopNoRefill = stock >= a;
                var last = k == 0;
                var nextSpbu = a - spbu;
                var nextCanSpbuNoRefill = stock >= (a + nextSpbu);
                var skipRefill = new Func<bool>(() =>
                {
                    if (last) return canStopNoRefill;
                    return nextCanSpbuNoRefill;
                });
                // simul
                stock -= spbu;  // to spbu
                assert(stock >= 0);
                if (skipRefill() == false)
                {
                    // refill
                    stock = b;
                    ans++;
                }
                stock -= (a - spbu);    // to stop
                assert(stock >= 0);
                // next loop
                spbu = a - spbu;
            }
            return ans;
        }

        int solve()
        {
            // a, b, f, k (0 < f < a ≤ e6, 1 ≤ b ≤ e9, 1 ≤ k ≤ e4)
            var A = ReadInt32();//e6
            var B = ReadInt32();//e6
            var F = ReadInt32();//e9
            var K = ReadInt32();//e4
            // only refill if necessary -> can't finish k
            return greedy(A, B, F, K);
            // Print the minimum number of times the bus needs to refuel to make k journeys. If it is impossible for the bus to make k journeys, print -1.
        }

        public void zStart()
        {
            Console.WriteLine(solve());

            ///////////////////////////////////////

            //foreach (var @__ in Enumerable.Range(1, ReadInt32()))
            //{
                // start... - no CONSOLE

                // end...

                //var ansLines = new List<string>(9999);
                //{
                //    // ANS start...

                //    //ansLines.AddRange(inp.Select(x => new string(x)));
                //    //...

                //    Console.Write("Case #{0}: ", @__);
                //    Console.WriteLine(string.Join(" ", __getFormattedString(ans)));
                //    ansLines.ForEach(x => Console.WriteLine(x));
                //}
                //Trace.WriteLine(string.Format("DONE_{0}.", @__));
            //}
        }

        T __getFormattedString<T>(T val) { return val; }
#if DEBUG
        string __getFormattedString(double d) { return d.ToString("0.000000"); }
#endif
        /* Tail starts here */
    }

#if TORIQ
    static void Pretest()
    {
        assert(Solution.greedy(6, 9, 2, 4), 4);
        assert(Solution.greedy(6, 10, 2, 4), 2);
        assert(Solution.greedy(2, 1, 1, 1), 1);
    }
    static void MainTest()
    {
        Test(File.ReadAllText("input.txt")
            , File.ReadAllText("output.txt")
            );
        //
    }
#endif

    // =============================================================================================

    #region faSTDIO
    [DebuggerNonUserCode]
    static void Main(String[] args)
    {
#if TORIQ
        zTestSuite();
        //new Solution().zInternalTestSuite();
        return;
#endif
#if GOOGLE
        zGoogle();
        return;
#endif
        new Solution().zStart();
    }

    static IEnumerable<char> Read()
    {
        int cin;
        var empty = true;
        while ((cin = Console.Read()) > -1)
        {
            var c = (char)cin;
            if (c == ' ' || c == '\n' || c == '\r' || c == '\t')
            {
                if (empty) continue;
                else break;
            }
            yield return c;
            empty = false;
        }
    }
    static T Read<T>(Converter<string, T> convert, int capacity = 20)
    { var temp = ReadString(capacity); return convert(temp); }
    static string ReadString(int capacity = 1000000)
    {
        capacity += Solution.PAD_FACTOR;
        var ans = new System.Text.StringBuilder(capacity);
        foreach (var c in Read()) ans.Append(c);
        return ans.ToString();
    }
    static string ReadLine(int capacity = 1000000)
    {
        capacity += Solution.PAD_FACTOR;
        var ans = Console.ReadLine();
        if (string.IsNullOrWhiteSpace(ans.Trim()))
            ans = Console.ReadLine();
        return ans;
    }
    static int ReadInt32() { return Read(Convert.ToInt32); }
    static long ReadLong64() { return Read(Convert.ToInt64); }

    #endregion
    #region Convert
    public static int int32(this long i64) { return Convert.ToInt32(i64); }
    public static int int32(this double dec64) { return Convert.ToInt32(dec64); }
    public static long long64(this int i32) { return (long)i32; }
    public static long long64(this double dec64) { return Convert.ToInt64(dec64); }
    public static double double64(this long i64) { return Convert.ToDouble(i64); }
    public static double double64(this int i32) { return (double)i32; }
    #endregion
    #region LIBS
    [DebuggerNonUserCode]
    public static void assert(bool mustBeTrue, string message = "ASSERTION FAILED", params object[] args)
    {
        if (mustBeTrue == false)
            throw new ApplicationException(string.Format(message, args));
    }

    [DebuggerNonUserCode]
    public static T assert<T>(T actual, T expected)
    {
        if (object.Equals(actual, expected) == false)
        {
            Debug.WriteLine(expected, "CORRECT");
            Debug.WriteLine(actual, "ACTUAL");
            assert(false, "[{0}] SHOULD BE [{1}]", actual, expected);
        }
        return actual;
    }

    [Conditional("DEBUG")]
    public static void EmptyConditionalDebugBreak() { }

    public static bool isLrLeftRight<T>(T left, T right) where T : IComparable<T>
    { return left.CompareTo(right) <= 0; }

    [DebuggerNonUserCode]
    public static void lrLeftRight_ASSERT<T>(T left, T right) where T : IComparable<T>
    { assert(isLrLeftRight(left, right), "Expect left [{0}] <= [{1}] right", left, right); }

    public static IEnumerable<T> forEach<T>(this IEnumerable<T> range, Action<T> action)
    { foreach (var elem in range) action(elem); return range; }

    [DebuggerNonUserCode]
    public static void __AssertIncrStep(double incrStep) { assert(incrStep > 0, "Expect 'incrStep [{0}] > 0'", incrStep); }

    public static long add64(long a, long b) { return a + b; }
    public static long mult64(long a, long b) { return a * b; }
    public static double div(double dividend, double divisor) { return dividend / divisor; }
    public static int ceil(double d) { return Convert.ToInt32(Math.Ceiling(d)); }
    public static int ceil(double dividend, double divisor) { return ceil(div(dividend, divisor)); }
    public static long ceil64(double d) { return Convert.ToInt64(Math.Ceiling(d)); }
    public static int floor(double d) { return Convert.ToInt32(Math.Floor(d)); }
    public static int floor(double dividend, double divisor) { return floor(div(dividend, divisor)); }
    public static long floor64(double d) { return Convert.ToInt64(Math.Floor(d)); }

    public static IEnumerable<int> rangeUntil(int left, int right, int incrStep = 1)
    {
        lrLeftRight_ASSERT(left, right);
        __AssertIncrStep(incrStep);
        for (var i = left; i <= right; i += incrStep) yield return i;
    }
    public static IEnumerable<int> rangeReverse(int left, int right, int decrStep = 1)
    {
        lrLeftRight_ASSERT(left, right);
        __AssertIncrStep(decrStep);
        for (var i = right; i >= left; i -= decrStep) yield return i;
    }

    #endregion

    // =============================================================================================

}


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

namespace Bus
{
    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(), b = Next(), f = Next(), k = Next();

            if (2*f > b || 2*(a - f) > b)
            {
                writer.WriteLine("-1");
                writer.Flush();
                return;
            }

            int count = 0;
            int x = b;
            bool to = true;
            for (int i = 1; i <= k; i++)
            {
                if (to)
                {
                    if (i != k)
                    {
                        if (a + (a - f) <= x)
                        {
                            x -= a;
                        }
                        else
                        {
                            count++;
                            x = b - (a - f);
                        }
                    }
                    else
                    {
                        if (x < a)
                            count++;
                    }
                }
                else
                {
                    if (i != k)
                    {
                        if (a + f <= x)
                        {
                            x -= a;
                        }
                        else
                        {
                            count++;
                            x = b - f;
                        }
                    }
                    else
                    {
                        if (x < a)
                            count++;
                    }
                }
                to = !to;
            }

            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;

namespace ConsoleApp1
{

    class Program
    {
        static void Main(string[] args)
        {
            int[] n = ((Console.ReadLine().Split(' ')).Select(int.Parse)).ToArray();
            int [] array = ((Console.ReadLine().Split(' ')).Select(int.Parse)).ToArray();
            int sum = 0;
            int prev = -1;
            for (int i = 0; i < n[0]/2; i++)
            {
                if (array[i] != array[n[0] - 1 - i])
                {
                    int s = array[i] + array[n[0] - 1 - i];
                    if (s == 1)
                    {
                        sum = -1;
                        break;
                    }
                    else
                    if (s == 2)
                    {
                      prev = 0;
                        sum += n[1];
                    }
                    else
                    {
                      prev = 1;
                        sum += n[2];
                    }
                }
                else if(array[i] == 2)
                {
                  if(prev == 1) sum += n[1];
                  else if(prev == 0) sum += n[2];
                  else if(n[1] < n2)
                  {
                    prev = 0;
                    sum += n[1];
                  }
                  else{ prev = 1; sum += n[2];}
                }
            }
            if (sum >= 0 && array[n[0] / 2] == 2)
                sum += n[0]%2==0 ?  2*  (int)Math.Min(n[1], n[2]) : (int)Math.Min(n[1], n[2]);
            Console.WriteLine(sum);
            Console.ReadLine();
        }

    }
}


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

namespace _1040A
{
    class Program
    {
        static void Main(string[] args)
        {
            IEnumerable<int> container = Console.ReadLine().Split().Select(int.Parse);
            int n = container.ElementAt(0), whiteCost = container.ElementAt(1), blackCost=container.ElementAt(2);
            List<int> colorslist = Console.ReadLine().Split().Select(int.Parse).ToList();
            int minCost = 0;
            if (n == 1 || !colorslist.Contains(2)) Console.WriteLine(0);
            else if (!CheckPalin(colorslist, n)) Console.WriteLine(-1);
            else
            {
                for (int i = 0; i < n / 2; i++)
                {
                    if (colorslist[i] == 1 && colorslist[n - 1] == 2 || colorslist[i] == 2 && colorslist[n - 1] == 1)
                        minCost += blackCost;
                    else if (colorslist[i] == 0 && colorslist[n - 1] == 2 || colorslist[i] == 2 && colorslist[n - 1] == 0)
                        minCost += whiteCost;
                    else
                    {
                        minCost += (Math.Min(whiteCost, blackCost)) * 2;
                    }
                }
                if (n % 2 == 1 && colorslist[n / 2 + 1] == 2) minCost += Math.Min(whiteCost, blackCost);
                Console.WriteLine(minCost);
            }
        }
        public static bool CheckPalin(List<int> colorslist,int n)
        {
            bool answer = true;
            for(int i=0;i<(n/2);i++)
            {
                if(colorslist[i]!=colorslist[n-1] && (colorslist[i]!=2 && colorslist[n-1]!=2))
                {
                    answer = false;
                    break;
                }
            }
            return answer;
        }
    }
}

        }
    }
}


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

namespace _630E_Rectangle
{
    class Program
    {
        static void Main(string[] args)
        {
            var temp = Array.ConvertAll(Console.ReadLine().Split(' '), x => Convert.ToInt32(x));
            int x1 = temp[0];
            int y1 = temp[1];
            int x2 = temp[2];
            int y2 = temp[3];

            int oddscount;
            int evencount;
            int oddsheight;
            int evenheight;

            if ((x2 - x1)%2 == 0)
            {
                if (x2%2 == 0)
                {
                    evencount = (x2 - x1)/2 + 1;
                    oddscount = evencount - 1;
                }
                else
                {
                    oddscount = (x2 - x1)/2 + 1;
                    evencount = oddscount - 1;
                }
            }
            else
            {
                oddscount = (x2 - x1 + 1)/2;
                evencount = oddscount;
            }

            oddsheight = (int)Math.Floor((y2 - y1)/2.0)+1;
            evenheight = (int) Math.Ceiling((y2 - y1)/2.0);

            Console.WriteLine(oddscount*oddsheight+evencount*evenheight);
        }
    }
}


using System;
class Program
{
    static void Main()
    {
        var p = Console.ReadLine();
        var w = new int[8];
        for (int i = 0; i < 10; i++)
        {
            var c = Console.ReadLine();
            var id = 0;
            for (int j = 0; j < 80; j += 10)
            {
                if ((id = p.Substring(j, 10).IndexOf(c)) >= 0)
                {
                    w[j] = i;
                    break;
                }
            }
        }
        foreach (var i in w)
        {
            Console.Write(i);
        }
    }
}


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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            Int32 i;
            string ans="NO", sInput;
            string fInput=Console.ReadLine();
            for(i=0; i<5; i++)
            {
               sInput = Console.ReadLine();
                if((fInput[0] == sInput[0]) || (fInput[0] == sInput[1]) || (fInput[1] == sInput[0]) || (fInput[1] == sInput[1]))
                {
                    ans = "YES";
                }
            }
            Console.WriteLine(ans);

//            Console.ReadKey();
        }
    }
}


using System;

namespace Contest
{
    class Scanner
    {
        private string[] line = new string[0];
        private int index = 0;

        public string Next()
        {
            if (line.Length <= index)
            {
                line = Console.ReadLine().Split(' ');
                index = 0;
            }

            var res = line[index];
            index++;
            return res;
        }

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

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

        public ulong NextUlong()
        {
            return ulong.Parse(Next());
        }

        public string[] Array()
        {
            line = Console.ReadLine().Split(' ');
            index = line.Length;
            return line;
        }

        public int[] IntArray()
        {
            var array = Array();
            var result = new int[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                result[i] = int.Parse(array[i]);
            }

            return result;
        }

        public long[] LongArray()
        {
            var array = Array();
            var result = new long[array.Length];
            for (int i = 0; i < array.Length; i++)
            {
                result[i] = long.Parse(array[i]);
            }

            return result;
        }
    }

    class Program
    {
        private string card;
        private string[] myCard;
        void Scan()
        {
            var sc = new Scanner();
            card = sc.Next();
            myCard = sc.Array();
        }

        public void Solve()
        {
            foreach (var s in myCard)
            {
                if (card[0] == s[0] || card[1] == s[1])
                {
                    Console.WriteLine("YES");
                    return;
                }

            }
            Console.WriteLine("NO");
        }

        static void Main() => new Program().Solve();
    }

}


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

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int n =Int32.Parse(Console.ReadLine());
            int m = Int32.Parse(Console.ReadLine());
            List<int> peoples = new List<int>();

            while (Console.ReadLine() != "")
            {
                peoples.Add(Int32.Parse(Console.ReadLine()));
            }

            int maxK = peoples.Max() + m;

            for (int i = 0; i < m; i++)
            {
                peoples[peoples.IndexOf(peoples.Min())]++;
            }

            int minK = peoples.Max();

            Console.WriteLine(minK + " " + maxK);
        }
    }
}


using System;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            int numBenches = Convert.ToInt32(Console.ReadLine());
            int numExtraPeople = Convert.ToInt32(Console.ReadLine());
            int modifiedNumExtraPeople = numExtraPeople;
            int[] benches = new int[numBenches];
            for (int i = 0; i < numBenches; i++) benches[i] = Convert.ToInt32(Console.ReadLine());
            int maxPeopleCount = FindMax(benches);
            foreach (var i in benches) modifiedNumExtraPeople -= maxPeopleCount - i;
            Console.WriteLine("{} {}", maxPeopleCount + Math.Ceiling((double)modifiedNumExtraPeople / numBenches), numExtraPeople + maxPeopleCount);
        }

        static int FindMax(int[] array)
        {
            int max = Int32.MinValue;
            foreach(var i in array)
            {
                if (i > max) max = i;
            }
            return max;
        }
    }
}


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

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string s = Console.ReadLine();
            string []ast = s.Split(new char[]{' '});
            int a = int.Parse(ast[0]);
            int b = int.Parse(ast[1]);
            int c = int.Parse(ast[2]);
            int n = int.Parse(ast[3]);

            int otv = n - ((a - c) + (b - c) + c);
            if((otv > 0) && (n > c))
            Console.WriteLine(otv);
            else
            Console.WriteLine("-1");
        }
    }
}


// Problem: 991A - If at first you don't succeed...
// Author: Gusztav Szmolik

using System;

class IfAtFirstYouDontSucceed
    {
    static int Main ()
        {
        string[] words = ReadSplitLine (4);
        if (words == null)
            return -1;
        byte a;
        if (!Byte.TryParse (words[0], out a))
            return -1;
        if (a > 100)
            return -1;
        byte b;
        if (!Byte.TryParse (words[1], out b))
            return -1;
        if (b > 100)
            return -1;
        byte c;
        if (!Byte.TryParse (words[2], out c))
            return -1;
        if (c > 100)
            return -1;
        byte n;
        if (!Byte.TryParse (words[3], out n))
            return -1;
        if (n > 100)
            return -1;
        sbyte ans = Convert.ToSByte (a >= c && b >= c ? a+b-c : -1);
        ans = Convert.ToSByte (ans > -1 && n > ans ? n-ans : -1);
        Console.WriteLine (ans);
        return 0;
        }

    static string[] ReadSplitLine (uint numWordNeed)
        {
        string line = Console.ReadLine ();
        if (line == null)
            return null;
        char[] delims = new char[2] {' ', '\t'};
        string[] words = line.Split (delims,StringSplitOptions.RemoveEmptyEntries);
        return (words.Length == numWordNeed ? words : null);
        }
    }


    static void Main()
    {
        string[] d = Console.ReadLine().Split(' ');
        int r = Convert.ToInt32(d[0]);
        int c = Convert.ToInt32(d[1]);
        bool b = false;
        int a=0;
        int ra = r;
        string[] s= new string[r];
        for (int i=0; i < r; i++) {
            s[i] = Console.ReadLine();
        }

        for (int i = 0; i < r; i++) {
            b = false;
            for (int j = 0; j < c; j++) {
                if (s[i][j] == 's') { b = true; break; }
            }
            if (!b)
            {
                a += c;
                ra--;
            }
        }

        for (int j = 0; j < c; j++)
        {
            b = false;
            for (int i = 0; i < r; i++)
            {
                if (s[i][j] == 's') { b = true; break; }
            }
            if (!b) a += ra;

        }
        Console.WriteLine(a);
    }
}


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

namespace fun
{
    class Program
    {
        private static TextReader cin = Console.In;
        private static TextWriter cout = Console.Out;
        static void Main(string[] args)
        {
            string s = cin.ReadLine() ;
            int a = int.Parse(s.Split(' ')[0]);
            var t = Array.ConvertAll(cin.ReadLine().Split(' '), int.Parse);
            var ar = new ArrayList(t);
            var br = new ArrayList(t);
            int ans = 100000000;
            int m = -100000000;

            for (int i = 1; i+1 < a; i++)
            {
                ar.RemoveAt(i);
                for(int j=0;j+1<ar.Count;j++)
                    m = Math.Max(m, (int)ar[j+1] - (int)ar[j]);
                ans = Math.Min(ans, m);
                ar = br;
            }
            cout.WriteLine(ans);

        }
    }
}


Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApplication1", "ConsoleApplication1\ConsoleApplication1.csproj", "{F9BBED00-742E-4EF5-8EC7-AE58D0427A2E}"
EndProject
Global
	GlobalSection(SolutionConfigurationPlatforms) = preSolution
		Debug|x86 = Debug|x86
		Release|x86 = Release|x86
	EndGlobalSection
	GlobalSection(ProjectConfigurationPlatforms) = postSolution
		{F9BBED00-742E-4EF5-8EC7-AE58D0427A2E}.Debug|x86.ActiveCfg = Debug|x86
		{F9BBED00-742E-4EF5-8EC7-AE58D0427A2E}.Debug|x86.Build.0 = Debug|x86
		{F9BBED00-742E-4EF5-8EC7-AE58D0427A2E}.Release|x86.ActiveCfg = Release|x86
		{F9BBED00-742E-4EF5-8EC7-AE58D0427A2E}.Release|x86.Build.0 = Release|x86
	EndGlobalSection
	GlobalSection(SolutionProperties) = preSolution
		HideSolutionNode = FALSE
	EndGlobalSection
EndGlobal


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

namespace CodeForces
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int step = 0;
            bool Cheak = false;
            if (n >= 1 && n <= Math.Pow(10, 9))
            {
                if (n == 1)
                {
                    Console.WriteLine(n);
                }
                else if (n == 2)
                {
                    Console.WriteLine(n-1);
                }
                else if (n % 2 == 0)
                {
                    for (; n != 0;)
                    {
                        if (Cheak == false)
                        {
                            n--;
                            step++;
                            if (n != 0)
                            Cheak = true;
                        }
                        if (Cheak == true)
                        {
                            n -= 2;
                            step++;
                            if (n != 0)
                            Cheak = false;
                        }
                    }
                    n = step;
                    Console.WriteLine(n);
                }
                else if ((n % 2 != 0))
                {
                    for (; n != 0;)
                    {
                        if (Cheak == true)
                        {
                            n--;
                            step++;
                            if (n != 0)
                            Cheak = false;
                        }
                        if (Cheak == false)
                        {
                            n -= 2;
                            step++;
                            if (n != 0)
                            Cheak = true;
                        }
                    }
                    n = step;
                    Console.WriteLine(n);
                }

            }
        }

    }
}


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

namespace CodeForces
{
    class Program
    {
        static void Main(string[] args)
        {
            int n = int.Parse(Console.ReadLine());
            int step = 0;
            bool Cheak = false;
            if (n >= 1 && n <= Math.Pow(10, 9))
            {
                if (n == 1)
                {
                    Console.WriteLine(n);
                }
                else if (n == 2)
                {
                    Console.WriteLine(n-1);
                }
                else if (n % 2 == 0)
                {
                    for (; n != 0;)
                    {
                        if (Cheak == false)
                        {
                            n--;
                            step++;
                            if (n != 0)
                            Cheak = true;
                        }
                        if (Cheak == true)
                        {
                            n -= 2;
                            step++;
                            if (n != 0)
                            Cheak = false;
                        }
                    }
                    n = step;
                    Console.WriteLine(n);
                }
                else if ((n % 2 != 0))
                {
                    for (; n != 0;)
                    {
                        if (Cheak == true)
                        {
                            n--;
                            step++;
                            if (n != 0)
                            Cheak = false;
                        }
                        if (Cheak == false)
                        {
                            n -= 2;
                            step++;
                            if (n != 0)
                            Cheak = true;
                        }
                    }
                    n = step;
                    Console.WriteLine(n);
                }

            }
            Console.ReadKey();
        }

    }
}


/*
csc -debug A.cs && mono --debug A.exe <<< "2
3
5
1
8"
*/

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

public class HelloWorld {
    public static void SolveCodeForces() {
        var a1 = cin.NI();
        var a2 = cin.NI();
        var k1 = cin.NI();
        var k2 = cin.NI();
        var n = cin.NI();

        var min = -1;
        var max = -1;

        for (var n1 = 0; n1 <= a1 * k1; n1++) {
            var n2 = n - n1;
            if (n2 < 0 || n2 > a2 * k2) continue;

            var mm1 = GetMinMax(a1, k1, n1);
            var mm2 = GetMinMax(a2, k2, n2);

            if (min < 0) {
                min = mm1[0] + mm2[0];
                max = mm1[1] + mm2[1];
            }
            min = Math.Min(min, mm1[0] + mm2[0]);
            max = Math.Max(max, mm1[1] + mm2[1]);
        }

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

    static int[] GetMinMax(int a, int k, int n) {
        var P = new int[a];
        var nn = n;
        for (var i = 0; i < a; i++) {
            var d = Math.Min(nn, k - 1);
            P[i] = d;
            nn -= d;
        }
// System.Console.WriteLine(new { a, k, n, P=P.Join(), nn });
        for (var i = 0; i < a; i++) {
            var d = Math.Min(nn, k - P[i]);
            P[i] += d;
            nn -= d;
        }
// System.Console.WriteLine(new { a, k, n, P=P.Join(), nn });
        var min = P.Count(p => p >= k);

        P = new int[a];
        nn = n;
        for (var i = 0; i < a; i++) {
            var d = Math.Min(nn, k);
            P[i] = d;
            nn -= d;
        }
        var max = P.Count(p => p >= k);
        return new[] { min, max };
    }

    static Scanner cin = new Scanner();
    static StringBuilder cout = new StringBuilder();

    static public void Main () {
        SolveCodeForces();
        // IncreaseStack(SolveCodeForces);
        // System.Console.Write(cout.ToString());
    }

    public static void IncreaseStack(ThreadStart action, int stackSize = 128000000)
    {
        var thread = new Thread(action, stackSize);
        thread.Start();
        thread.Join();
    }
}

public static class Helpers {
    public static string Join<T>(this IEnumerable<T> arr, string sep = " ") {
        return string.Join(sep, arr);
    }

    public static void Swap<T>(ref T a, ref T b) {
        var tmp = a;
        a = b;
        b = tmp;
    }

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

public class Scanner
{
    private string[] line = new string[0];
    private int index = 0;

    public string Next() {
        if (line.Length <= index) {
            line = Console.ReadLine().Split(' ');
            index = 0;
        }
        var res = line[index];
        index++;
        return res;
    }

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

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

    public ulong NUL() {
        return ulong.Parse(Next());
    }

    public string[] Array() {
        line = Console.ReadLine().Split(' ');
        index = line.Length;
        return line;
    }

    public int[] NIA(int emptyPrefixLen = 0) {
        var array = Array();
        var result = new int[emptyPrefixLen + array.Length];
        for (int i = 0; i < array.Length; i++)
            result[emptyPrefixLen + i] = int.Parse(array[i]);
        return result;
    }

    public long[] NLA() {
        var array = Array();
        var result = new long[array.Length];
        for (int i = 0; i < array.Length; i++)
            result[i] = long.Parse(array[i]);
        return result;
    }

    public ulong[] NULA() {
        var array = Array();
        var result = new ulong[array.Length];
        for (int i = 0; i < array.Length; i++)
            result[i] = ulong.Parse(array[i]);
        return result;
    }
}