import com.sun.deploy.util.ArrayUtil;

import java.io.*;
import java.util.*;

public class Main {
    static int  n;
    static int m;
    static int[]a;
    static int start;
    static int end;
    static int ans;
    public static void main(String[] args) throws Exception
    {
        Scanner in = new Scanner(System.in);
        OutputWriter out = new OutputWriter(System.out);
         n  = in.nextInt();
        a = new int[n];
         m = in.nextInt();
        for (int i =0;i<n;i++)
            a[i]=in.nextInt();
       Arrays.sort(a);
      reverse(a);
       ans=-1;
       binarySearch();
       out.printLine(ans);
        out.flush();
    }
    static void reverse(int[]array)
    {
        for(int i=0; i<array.length/2; i++){ int temp = array[i]; array[i] = array[array.length -i -1]; array[array.length -i -1] = temp; }

    }
    static void binarySearch()
    {
         start = 1;
         end = n;
        while (end>start)
        {
            int mid = (end+start)/2;
            if (f(mid))
            {
                ans=mid;
                end=mid-1;
            }
            else
                start=mid+1;
        }

    }
    static boolean f(int y)
    {
        int ans=0;
        for (int i=0;i<n;i++)
            ans+=Math.max(0,a[i]-i/y);
        if (ans>=m)
            return true;
        return false;
    }
    static void MergeSort(int[] a, int p, int r)
    {
        if (p < r)
        {
            int q = (r + p) / 2;
            MergeSort(a,   p, q);
            MergeSort(a,  q + 1, r);
            Merge(a,  p, q, r);
        }
    }
    static void Merge(int[] a,int p, int q, int r)
    {
        int n1 = q - p + 1;
        int n2 = r - q;
        int[] R = new int[n1 + 1];
        int[] L = new int[n2 + 1];

        for (int i = 0; i < n1; i++)
        {
            R[i] = a[p + i];

        }
        R[n1] = Integer.MAX_VALUE;
        for (int i = 0; i < n2; i++)
        {
            L[i] = a[q + i + 1];

        }
        L[n2] =Integer.MAX_VALUE;
        int n = a.length;
        int j = 0;
        int k = 0;
        for (int i = p; i <= r; i++)
        {
            if (L[j] < R[k])
            {
                a[i] = L[j];

                j++;
            }
            else
            {
                a[i] = R[k];

                k++;
            }

        }
    }
}
class Graph {

    int n;
    ArrayList<Integer>[] adjList;

    public Graph(int n) {
        this.n = n;
        adjList = new ArrayList[n];
        for (int i = 0; i < n; i++)
            adjList[i] = new ArrayList<>();
    }

}
class pair  implements Comparable
{

    int key;
    int value;
    int id;
    public pair(Object key, Object value,Object id) {

        this.key = (int)key;
        this.value=(int)value;
        this.id=(int)id;
    }

    @Override
    public int compareTo(Object o) {
        pair temp =(pair)o;
        if (key!=temp.key)
            return  key-temp.key;
        else return id-temp.id;
    }
}
class Scanner {

    StringTokenizer st;
    BufferedReader br;

    public Scanner(InputStream s) {
        br = new BufferedReader(new InputStreamReader(s));
    }

    public Scanner (FileReader f) {
        br = new BufferedReader(f);
    }

    public String next() throws IOException {
        while (st == null || !st.hasMoreTokens())
            st = new StringTokenizer(br.readLine());
        return st.nextToken();
    }

    public int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    public long nextLong() throws IOException {
        return Long.parseLong(next());
    }

    public String nextLine() throws IOException {
        return br.readLine();
    }

    public double nextDouble() throws IOException {
        String x = next();
        StringBuilder sb = new StringBuilder("0");
        double res = 0, f = 1;
        boolean dec = false, neg = false;
        int start = 0;
        if (x.charAt(0) == '-') {
            neg = true;
            start++;
        }
        for (int i = start; i < x.length(); i++)
            if (x.charAt(i) == '.') {
                res = Long.parseLong(sb.toString());
                sb = new StringBuilder("0");
                dec = true;
            } else {
                sb.append(x.charAt(i));
                if (dec)
                    f *= 10;
            }
        res += Long.parseLong(sb.toString()) / f;
        return res * (neg ? -1 : 1);
    }

    public boolean ready() throws IOException {
        return br.ready();
    }

    public boolean nextEmpty() throws IOException {
        String s = nextLine();
        st = new StringTokenizer(s);
        return s.isEmpty();
    }
}
class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer) {
        this.writer = new PrintWriter(writer);
    }

    public void print(Object... objects) {
        for (int i = 0; i < objects.length; i++) {
            if (i != 0)
                writer.print(' ');
            writer.print(objects[i]);
        }
    }

    public void printLine(Object... objects) {
        print(objects);
        writer.println();
    }

    public void close() {
        writer.close();
    }

    public void flush() {
        writer.flush();
    }

}


import java.util.Arrays;
import java.util.Scanner;

public class Run {

	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		String wholeInput = input.nextLine();
		String[] token = wholeInput.split(" ");
		int n = Integer.valueOf(token[0]);
		int m = Integer.valueOf(token[1]);
		wholeInput = input.nextLine();
		token = wholeInput.split(" ");
		int[] a = new int[n];
		for (int i = 0; i < n; i++)
		{
			a[i] = Integer.valueOf(token[i]);
		}
		input.close();
		token = null;
		int S = 0;
		for (int i = 0; i < n; i++)
		{
			S += a[i];
		}
		if (m > S)
		{
			System.out.print(-1);
		}
		else
		{
			System.out.print(solve(0, m, m, a));
		}
	}

	private static boolean checkAnswer(int m, int day, int[] a)
	{
		for (int i = 0; i < a.length; i++)
		{
			a[i] = -a[i];
		}
		Arrays.sort(a);
		for (int i = 0; i < a.length; i++)
		{
			a[i] = -a[i];
		}
		int arrayMarker = 0;
		int cupMarker = 0;
		int S = 0;
		while(true)
		{
			if (a[arrayMarker] - cupMarker > 0)
			{
				S += a[arrayMarker] - cupMarker;
			}
			arrayMarker++;
			if (arrayMarker % day == 0)
			{
				cupMarker++;
			}
			if (arrayMarker == a.length)
			{
				break;
			}
		}
		if (S >= m)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	private static int solve(int tempPos, int length, int m, int[] a)
	{
		int checkPos = tempPos + length / 2;
		if (length == 0)
		{
			return tempPos;
		}
		if (checkAnswer(m, checkPos, a))
		{
			return solve(tempPos, checkPos - tempPos, m, a);
		}
		else
		{
			return solve(checkPos + 1, tempPos + length - checkPos - 1, m, a);
		}
	}

}


//Problem 2
//Codeforces 32C
//Meg Rydson, Brian Chren, Chris Johnson

import java.util.*;
import java.io.*;

public class flea {
    public static void main(String[] args) throws IOException{
        Input.init( System.in );
        PrintWriter out = new PrintWriter(System.out);
        int n = Input.nextInt(); // length of the board
        int m = Input.nextInt(); // width of the board
        int s = Input.nextInt(); // length of the flea's jump

        int output = ((1 + (n-1)/s) * (1 + (m -1)/s)) * ((1 + (n-1) % s) * (1 + (m -1) % s));

        out.println(output);

        out.close();
    }
}


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Scanner;

public class Flea {
	public static void main(String[] args) {

		//try {
		//	System.setIn(new FileInputStream("in.test"));
		//	System.setOut(new PrintStream(new FileOutputStream("out.txt")));
		//} catch (FileNotFoundException e) {
		//	e.printStackTrace();
		//}
		Scanner cin = new Scanner(System.in);
		while (cin.hasNext()) {
			int n = cin.nextInt();
			int m = cin.nextInt();
			int s = cin.nextInt();
			int sn = (n - 1)/s + 1;
			int sm = (m - 1)/s + 1;
			int nn = n % s;
			if (nn == 0) {
				nn = s;
			}
			int mm = m % s;
			if (mm == 0) {
				mm = s;
			}
			System.out.println(sn * sm * nn * mm);
		}
	}
}


import java.util.Scanner;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author andrey
 */
public class Flea {

    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();
        int s = in.nextInt();
        if(n<m){
            int temp = n;
            n = m;
            m=temp;
        }
        if(n>s && m>s){
            int a = (n-1)/s+1;
            int b = (m-1)/s+1;
            int lefta = (n-1)%s+1;
            int leftb =  (m-1)%s+1;
            System.out.println(a*lefta*b*leftb);
        }
        else if(m<=s && n>s){
            int a = (n-1)/s+1;
            int lefta = (n-1)%s+1;
            System.out.println(a*lefta*m);
        }else{
            System.out.println(n*m);
        }
    }

}


import java.util.*;
import java.io.*;
import static java.lang.Math.*;
import java.math.*;

public class Main implements Runnable {
	BufferedReader in;
	PrintStream out;
	StringTokenizer st = new StringTokenizer("");

	public static void main(String [] args) throws Exception {
		new Thread(new Main()).start();
	}

	void printExit(String s) {
		System.out.println(s);
		System.exit(0);
	}

	public void run() {
		try {
			Locale.setDefault(Locale.US);
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintStream(System.out);
			int w = nextInt(), h = nextInt(), s = nextInt();
			int [] a = new int [] {w, h};
			int [] good = new int[2];
			for (int i = 0; i < 2; i++) {
				int r = (a[i] - 1) % s + 1;
				for (int j = 0; j < a[i]; j++)
					if (j % s < r) good[i]++;
			}
			out.println(good[0] * good[1]);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	 boolean seekForToken() {
 		try {
			while (!st.hasMoreTokens()) {
				String s = in.readLine();
				if (s == null) {
				 	return false;
				}
			 	st = new StringTokenizer(s);
			}
			return true;
		}
		catch (IOException e) {
		 	e.printStackTrace();
		 	return false;
		}
 	}

 	 int nextInt() {
		return Integer.parseInt(nextToken());
 	}

 	 long nextLong() {
		return Long.parseLong(nextToken());
 	}

 	 double nextDouble() {
		return Double.parseDouble(nextToken());
 	}

 	 BigInteger nextBigInteger() {
 	 	return new BigInteger(nextToken());
 	}

 	 String nextToken() {
 	 	seekForToken();
 	 	return st.nextToken();
 	}

}


import java.io.*;
public class abv {
public static void main(String[]args)throws IOException
{
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
int c=0;int c1=0;
int n=Integer.parseInt(buf.readLine());
for(int j=2;j<=n;j++)
{
for(int i=2;i<=j;i++)
{
if(isprime(i)&&n%i==0)
c++;
}
if(c==2)
c1++;
}
System.out.println(c1);
}
public static boolean isprime(int a)
{
int l=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
l++;
}
if(l==2)
return true;
else
return false;
}
}


import java.io.*;
public class abv {
public static void main(String[]args)
{
BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
int c=0;int c1=0;
int n=Integer.parseInt(buf.readLine());
for(int j=2;j<=n;j++)
{
for(int i=2;i<=j;i++)
{
if(isprime(i)&&n%i==0)
c++;
}
if(c==2)
c1++;
}
System.out.println(c1);
}
public static boolean isprime(int a)
{
int l=0;
for(int i=1;i<=n;i++)
{
if(n%i==0)
l++;
}
if(l==2)
return true;
else
return false;
}
}


import java.util.*;

public class Almost_prime_26A
{
    public static void func(int n)
    {
        int []prime = new int[n+1];
        Arrays.fill(prime, 0);
        for(int i=2; i*i<MAX; i++)
        {
            if(prime[i] == 0)
            {
                for(int j=i; i*j <= n; j++)
                    prime[i*j]++;
            }
        }
        int res = 0;
        for(int i=0; i<=n; i++)
        {
            if(prime[i] == 2)
                res++;
        }
        System.out.println(res);
    }

    public static void main(String args[])
    {

        Scanner scan = new Scanner(System.in);

        int tests = scan.nextInt();

        while(tests-- > 0)
        {
          int num = scan.nextInt();
          func(num);
        }
    }
}


import java.util.Scanner;

public class AlmostPrime
{
	public static void main(String[] args)
	{
		Scanner scan=new Scanner(System.in);
		int threshHold=scan.nextInt();
		int num=2,prime=2;
		int count=0;
		int flag=0;
		int res=0;
		int temp=-1;
	  	while(num<=threshHold)
			{

			  if(num%prime==0 &&num>=prime)
			   {
				 if(temp==-1)
				 {
					 temp=num/prime;
				 }
				 else
				 {
					 temp=temp/prime;
				 }
			     prime=getPrime(prime+1);
				 count++;

				 if(count==2)
				 {
`					 while(prime<=temp)
					 {
					  if(num%getPrime(prime)==0)
					  {
					   flag=1;
					   break;
					  }
					  prime=prime+1;
					 }
					 temp=-1;
					 if(flag==0)
					 {
						 res=res+1;
					 }
					 num++;
					 count=0;
					 prime=2;
					 flag=0;
				 }
			   }
			  else if(num<prime)
			   {
				  num++;
				  count=0;
				  prime=2;
				  temp=-1;
			   }
			  else if(num%prime>=1)
			   {
				  prime=getPrime(prime+1);
			   }
			  else
			  {
//				  System.out.println(prime+" "+num);
			  }
			}
		System.out.println(res);

		scan.close();

	}
	static int getPrime(int num)
	{
		int count=2;
		if(num<2)
		{
			return 0;
		}
		while(true)
		{
			if(num%count==0 && num>count)
			{
				count=2;
				num++;
			}
			else if(count==num)
			{
				break;
			}
			else
			{
				count++;
			}
		}
		return num;
	}

}


import java.util.*;
public class R311 {
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		int N = Integer.parseInt(sc.nextLine()); int[] mins = new int[3], maxes = new int[3];
		for(int i = 0; i < 3; i++){
			StringTokenizer s = new StringTokenizer(sc.nextLine());
			mins[i] = Integer.parseInt(s.nextToken());
			maxes[i] = Integer.parseInt(s.nextToken());
		} int one=mins[0],two=mins[1],three=mins[2];
		int start = mins[0]+mins[1]+mins[2];
		while(start < N && one < maxes[0]){start++;one++;}
		while(start < N && two < maxes[1]){start++;two++;}
		while(start < N && three < maxes[2]){start++;three++;}
		System.out.println(one + " " + two + " " + three);
	}
}
	}
}


cimport java.util.Scanner;
public class IlyaandDiplomas {
public static void main(String args[]){
    Scanner s=new Scanner(System.in);
    int n=s.nextInt();
    //s.nextLine();
    int min1,max1,min2,max2,min3,max3;
    min1=s.nextInt();
    max1=s.nextInt();
    //s.nextLine();
    min2=s.nextInt();
    max2=s.nextInt();
    //s.nextLine();
    min3=s.nextInt();
    max3=s.nextInt();
    int s1=0,s2=0,s3=0;
    int intermediateone=n;
    if(intermediateone-min2-min3>=max1){
        s1=max1;
        intermediateone-=max1;
        if(intermediateone-min3>=max2){
            s2=max2;
            intermediateone-=max2;
            if(intermediateone>=max3){
                s3=max3;
            }
                else {
                    s3=intermediateone;
                }
        }
        else{
            s2=intermediateone-min3;
            s3=min3;
        }
    }
    else {
          s1=intermediateone-min2-min3;
          s2=min2;
          s3=min3;
    }
    System.out.println(s1+" "+s2+" "+s3);
}
}


public class Main {

    public static void main(String[] args) {

    public int i,j,l;
    public int minFirts, maxFirst, minSecond, maxSecond, minThird, maxThird;
    public int summ;

    public RunProgramm(){
        Scanner in = new Scanner(System.in);
        summ = in.nextInt();
        minFirts = in.nextInt();
        maxFirst = in.nextInt();
        minSecond = in.nextInt();
        maxSecond = in.nextInt();
        minThird = in.nextInt();
        maxThird = in.nextInt();

        i = minFirts;
        j = minSecond;
        l = minThird;

        while ((i+j+l)<summ) {
            if (i<maxFirst){
                i++;
            }
            else if(j<maxSecond){
                j++;
            }
            else {l++;}

        }
        System.out.println(i);
        System.out.println(j);
        System.out.println(l);
    }

}
}


import java.util.*;
public class Main
{
public static void main (String args[])
{
   Scanner in = new Scanner(System.in);
   int n=in.NextInt();
   int min1=in.NextInt();
   int max1=in.NextInt();
   int min2=in.NextInt();
   int max2=in.NextInt();
   int min3=in.NextInt();
   int max3=in.NextInt();

   while(min1+min2+min3<n&&min1<max1)min1++;
   while(min1+min2+min3<n&&min2<max2)min2++;
   while(min1+min2+min3<n&&min3<max3)min3++;

System.out.Println(min1+" "+min2+" "+min3);

}
}


import jdk.dynalink.beans.StaticClass;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.Collections;
import java.io.InputStreamReader;
import java.util.*;

public class solution{

    public static void main(String[] args) {
    FastScanner sc = new FastScanner() ;
    int n = sc.nextInt() ;
    int s = sc.nextInt() ;
    int cups[] = sc.readArray(n) ;
    Arrays.sort(cups);
    int count = 0 ;
    for (int i=0 ; i<n-1 ; i++){
        count += cups[i] ;
    }
    if (count <= s)
        System.out.println("YES");
    else
        System.out.println("NO");
    }

    static final Random random=new Random();

    static void ruffleSort(int[] a) {
        int n=a.length;//shuffle, then sort
        for (int i=0; i<n; i++) {
            int oi=random.nextInt(n), temp=a[oi];
            a[oi]=a[i]; a[i]=temp;
        }
        Arrays.sort(a);
    }

    static class FastScanner {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer("");
        String next() {
            while (!st.hasMoreTokens())
                try {
                    st=new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }
        int[] readArray(int n) {
            int[] a=new int[n];
            for (int i=0; i<n; i++) a[i]=nextInt();
            return a;
        }
        long nextLong() {
            return Long.parseLong(next());
        }
    }

}
// FastScanner credits - SecondThread


public class main{

    public static void main(String[] args) {

        Scanner s =new Scanner(System.in);

        int n,l;

        n=s.nextInt();
        l=s.nextInt();

        int sum=0;
        boolean b =true;

        int arr[] =new int[n];

        for(int i=0;i<arr.length;i++)
            arr[i]=s.nextInt();

        Arrays.sort(arr);

        for(int i=0;i<arr.length-1;i++)
              sum+=arr[i];

        if(sum<=l)
            System.out.println("YES");
        else
            System.out.println("NO");

    }

}


import java.util.Scanner;
public class Main{
    public static void main(String[] args) {
        Scanner cin = new Scanner(System.in);
        String s;
        s = cin.next();
        if(s.length()==1){
            System.out.print(s.charAt(0));
            return;
        }

        if(s.length() % 2 == 0){
            System.out.print(s.charAt(s.length()/2-1));
            for(int i=1;i<=s.length()/2-1;i++)
            {
                System.out.print(s.charAt(s.length()/2-1 + i));
                System.out.print(s.charAt(s.length()/2-1 - i));
            }
            System.out.print(s.charAt(s.length()-1));
        }
        else{
            System.out.print(s.charAt(s.length()));
            for(int i=1;i<s.length();i++){
                System.out.print(s.charAt(s.length()/2 + i));
                System.out.print(s.charAt(s.length()/2 - i));
            }

        }


import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;

public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        TaskA solver = new TaskA();
        solver.solve(1, in, out);
        out.close();
    }
}
class TaskA {
    public void solve(int testNumber, InputReader in, PrintWriter out) {
        int n = in.nextInt();
        int s = in.nextInt();
        int [] a = new int [n];
        for (int  i  = 1; i <= n; ++i){
            a[i] = in.nextInt();
        }
        Arrays.sort(a);
        int sum = 0;
        for (int i = 1; i < n; ++i) sum += a[i];
        if (sum <= s ) out.printl("YES");
        else out.printl("NO");
    }
}
    class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

    }


public class EXE {
public static void main(String []args)
{
	int a,b;
	if(b>=a&&b-a<=1) System.out.println("YES");
	else  System.out.println("NO")
}
}


import java.util.*;

public class Main {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		int a = sc.nextInt(),b=sc.nextInt();

		System.out.print(Math.abs(a-b)<=1? "YES" : "NO");
}


import hava.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int a = in.nextInt();
        int b = in.nextInt();
        if(a==b || b==a+1 || a==b+1)
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}


import java.util.*;
public class Dasha_and_Stairs {

	public static void main(String[] args) {
		Scanner out = new Scanner(System.in);

		int a = out.nextInt();
		int b = out.nextInt();

		if ((a==b+1)||(a==b-1)||(a==b+1)||(a==b-1)||(a==b) {
			System.out.println("YES");
		}
		else  System.out.println("NO");

	}

}


import java.util.*;
import java.io.*;
import java.lang.*;
public class test5 {

	public static void main(String args[]){
		 MyScanner sc = new MyScanner();
	     out = new PrintWriter(new BufferedOutputStream(System.out));
	     int initlength = sc.nextInt();
	     int finlength = sc.nextInt();
	     int holder = 0;
	     int seconds = 0;
	     int[] sidelengths = new int[3];
	     if(initlength < finlength){
	    	 sidelengths[0] = initlength;
	    	 sidelengths[1] = initlength;
	    	 sidelengths[2] = initlength;
	     }
	     else{
	    	 sidelengths[0] = finlength;
	    	 sidelengths[1] = finlength;
	    	 sidelengths[2] = finlength;
	    	 holder = initlength;
	    	 initlength = finlength;
	    	 finlength = holder;
	     }
	     while(sidelengths[0] != finlength){
	    	 System.out.println("hello");
	    	 if(finlength < 2*sidelengths[2])
	    	 {
	    		 seconds += 3;
	    		 sidelengths[0] = finlength;
	    	 }
	    	 else if(finlength == 2*sidelengths[2]){
	    		 seconds += 4;
	    		 sidelengths[0] = finlength;
	    	 }
	    	 {
	    		 sidelengths[0] = sidelengths[1] + sidelengths[2] - 1;
	    		 sidelengths[1] = sidelengths[0] + sidelengths[2] - 1;
	    		 sidelengths[2] = sidelengths[0] + sidelengths[1] - 1;
	    		 seconds += 3;
	    	 }
	     }
	         System.out.println(seconds);

	  }

	  //-----------PrintWriter for faster output---------------------------------
	  public static PrintWriter out;

	  //-----------MyScanner class for faster input----------
	  public static class MyScanner {
	     BufferedReader br;
	     StringTokenizer st;

	     public MyScanner() {
	        br = new BufferedReader(new InputStreamReader(System.in));
	     }

	     String next() {
	         while (st == null || !st.hasMoreElements()) {
	             try {
	                 st = new StringTokenizer(br.readLine());
	             } catch (IOException e) {
	                 e.printStackTrace();
	             }
	         }
	         return st.nextToken();
	     }
	     int nextInt() {
	         return Integer.parseInt(next());
	     }
	     long nextLong() {
	         return Long.parseLong(next());
	     }
	     double nextDouble() {
	         return Double.parseDouble(next());
	     }
	     String nextLine(){
	         String str = "";
		  try {
		     str = br.readLine();
		  } catch (IOException e) {
		     e.printStackTrace();
		  }
		  return str;
	     }
	  }
	 }
	/*
	int n      = sc.nextInt();        // read input as integer
	long k     = sc.nextLong();       // read input as long
	double d   = sc.nextDouble();     // read input as double
	String str = sc.next();           // read input as String
	String s   = sc.nextLine();       // read whole line as String
	*/


/*
 * Code Author: Mayank Kumar
 * Institute  : DA-IICT
 * 3rd year BTech ICT Student
 */

import java.io.*;
import java.util.*;

public class CFEDU15E
{
	static int[][] pa,min;
	static long[][] sum;
	private static int[] nextIntArray(InputReader in,int n)
	{
		int[] a=new int[n];
		for(int i=0;i<n;i++)
			a[i]=in.nextInt();
		return a;
	}
	private static void show2DArray(int[][] a)
	{
		for(int i=0;i<a.length;i++)
		{
			for(int j=0;j<a[0].length;j++)
				System.out.print(a[i][j]+" ");
			System.out.println();
		}
	}
	private static void show2DArray(long[][] a)
	{
		for(int i=0;i<a.length;i++)
		{
			for(int j=0;j<a[0].length;j++)
				System.out.print(a[i][j]+" ");
			System.out.println();
		}
	}
	public static void main(String[] args)
	{
		InputReader in = new InputReader(System.in);
		OutputStream outputStream = System.out;
	//	PrintWriter out = new PrintWriter(outputStream);
		//int n,i=0,lim=35,j,t;
		//long k;
		int n=in.nextInt();
		int m=in.nextInt();
		long s=0;
		int []a=new int[3];
		a[0]=m;
		a[1]=m;
		a[2]=m;
		//int[] a=nextIntArray(in, n);
		if(n/2 ==m){
			System.out.println("4");
			return ;
		}
			if(n/2 <m){
			System.out.println("3");
			return ;
		}
	for( ;;){
		if(a[0]==n)
		break;
		s++;
		if(a[1]==n && a[2]==n){
		a[0]=n;
		continue;
		}
		if(a[0]+a[2]>n){
			a[1]=n;
			Arrays.sort(a);
			continue;
		}
		a[0]=a[1]+a[2]-1;
		Arrays.sort(a);
	}
	System.out.println(s);
	}
	static class InputReader {
		private final InputStream stream;
		private final byte[] buf = new byte[8192];
		private int curChar, snumChars;
		private SpaceCharFilter filter;

		public InputReader(InputStream stream) {
			this.stream = stream;
		}

		public int snext() {
			if (snumChars == -1)
				throw new InputMismatchException();
			if (curChar >= snumChars) {
				curChar = 0;
				try {
					snumChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (snumChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public int nextInt() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public long nextLong() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = snext();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = snext();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

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

		public String readString() {
			int c = snext();
			while (isSpaceChar(c)) {
				c = snext();
			}
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = snext();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public boolean isSpaceChar(int c) {
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public interface SpaceCharFilter {
			public boolean isSpaceChar(int ch);
		}
	}
}


import java.util.*;
import java.lang.*;
import java.math.*;

public class CF712C {
  public static void main(String args[]) {
    Scanner s = new Scanner(System.in);
    int x = s.nextInt();
    int y = s.nextInt();
    int res = devolve(x, y);
    System.out.println(res);
  }

  public static int devolve(int x, int y) {
    int res = 0;
    int a = y;
    int b = y;
    int c = y;

    // All must Increment up to x
    while (a != x || b != x || c != x) {
      // Vouga conditions
      for (int i = 0; i < 3; i++) {

      //a + b > c
        if (i == 0) {
          c = a + b - 1;
        }

      // b + c > a
        if (i == 1) {
          a = b + c - 1;
        }

      // c + a > b
        if (i == 2) {
          b = c + a - 1;
        }
        res++;
      }

    }

    return res;
  }

}


// package CodeForces;

import java.io.*;
import java.util.*;

public class Problem_712C {
	public static int get(int[]d1,int sum)
	{
		int[]d=d1.clone();
		if(d[2]==y)
		{
			return sum;
		}
//		if(sol[d[1]][d[2]]<=sum)
//			return sol[d[1]][d[2]];
		int x=Math.max(d[1]-d[0]+1,y);
		d[2]=x;
		Arrays.sort(d);
		return get(d,sum+1);
	}
	static int[][]sol;static int y;
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner();
		PrintWriter pw = new PrintWriter(System.out);
		int x=sc.nextInt();y=sc.nextInt();
		sol=new int[x+1][x+1];
		int[]a=new int[3];
		Arrays.fill(a, x);int min=Integer.MAX_VALUE;
		while(a[0]>=y)
		{
			a[0]--;
			min=Math.min(min, get(a,0));
		}
		System.out.println(min+1);
		pw.close();
	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		Scanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		String next() throws IOException {
			while (st == null || !st.hasMoreTokens()) {
				st = new StringTokenizer(br.readLine());
			}
			return st.nextToken();
		}

		int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		long nextLong() throws IOException {
			return Long.parseLong(next());
		}

		double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}

		String nextLine() throws IOException {
			return br.readLine();
		}

		boolean hasnext() throws IOException {
			return br.ready();
		}

	}
}


import java.util.*;

public class BoringApartments{
    public static void main(String[] args){
        try{
            Scanner scan = new Scanner(System.in);
            int t,n,i,j,temp,ans;
            t = scan.nextInt();

            for(i=0; i<t; i++){
                t = scan.nextInt();
                if(t < 10){
                    temp = 1;
                    if(t == 1)
                        ans = 1;
                    else{
                        while(temp < t){
                            ans += 4 + 3 + 2 + 1;
                            temp++;
                        }
                        ans++;
                    }

                }else if(t < 100){
                    temp = 11;
                    if(t == 11)
                        ans = 2+1;
                    else{
                        while(temp < t){
                            ans += 4 + 3 + 2 + 1;
                            temp += 11;
                        }
                        ans++;
                    }
                }else if(t < 1000){
                    temp = 111;
                    if(t == 111)
                        ans = 3+2+1;
                    else{
                        while(temp < t){
                            ans += 4 + 3 + 2 + 1;
                            temp += 111;
                        }
                        ans++;
                    }
                }else if(t < 10000){
                    temp = 1111;
                    if(t == 1111)
                        ans = 4+3+2+1;
                    else{
                        while(temp < t){
                            ans += 4 + 3 + 2 + 1;
                            temp += 1111;
                        }
                        ans++;
                    }
                }
                System.out.println(ans);
            }
            scan.close();

        } catch(Exception e){
            e.printStackTrace;
        }
    }
}


import java.util.*;
import java.lang.*;

public class Named
{
   public static void main(String args[])
   {

      Scanner in = new Scanner(System.in);
      int nInputs = in.nextInt();
      int[] arr = new int[nInputs];
      for(int i=0;i<nInputs;i++){
          arr[i] = in.nextInt();
      }
      int count =0;
      for(int i=0;i<nInputs;i++){
          int newLen = arr[i].length();
          int len = (newLen+1)*newLen/2;
          int day = arr[i]%10;
        switch (day) {
          case 1:
            count =  len;
            System.out.println(count);
            break;
          case 2:
            count =  len + 10;
            System.out.println(count);
            break;
          case 3:
            count = len + 20;
            System.out.println(count);
            break;
          case 4:
            count = len + 30;
            System.out.println(count);
            break;
          case 5:
            count = len + 40;
            System.out.println(count);
            break;
          case 6:
            count = len + 50;
            System.out.println(count);
            break;
          case 7:
            count = len + 60;
            System.out.println(count);
            break;
          case 8:
            count = len + 70;
            System.out.println(count);
            break;
          case 9:
            count = len + 80;
            System.out.println(count);
            break;
        }

      }

   }

}


import java.util.Scanner;

public class Solution {
    public static void main(String args[]) throws Exception
    {
        Scanner ss = new Scanner(System.in);

        // ArrayList<Integer> arr = new ArrayList<Integer>();
        // for(int i=0;i<arr.size();i++){
        //     arr.set(i, arr.get(i)*2);
        // }

            int t = ss.nextInt();

        while(t>0){
            int n = ss.nextInt();
//            int a[] = new int[n];
//            int b[] = new int[n];
//            int c[] = new int[n];
//

            int sum = 0;
            for(int i=1;i<n%10;i++)
                sum +=10;
            for(int i=1;i<=(n+"").length();i++)
                sum +=i;
//            for(int i=0;i<n;i++)
//                c[i] = ss.nextInt();
//
//            getResult(n,a,b,c);
            System.out.println(sum);
            t--;

        }

    }


public class Hello
{
 public static void main(String[] args)
 {
  Scanner sc=new Scanner(System.in);
  while(sc.hasNext())
  {
   int n=sc.nextInt();
   for(int i=1;i<=n;i++)
   {
    int o=sc.nextInt();
     int d1=0;
     while(o!=0)
     {
      int d=(int)o/10;
      d1++;
      o=(int)o/10;
     }
     int kp = (10*(d-1))+ (d1*(d1+1)/2);
     System.out.println(kp);
    }
   }
  }
 }


import java.io.*;
import java.util.*;

public class D {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int t = s.nextInt();
		int[] nos = new int[n];
		for (int i=0; i<n; i++) {
			nos[i] = s.nextInt();
		}
		int[] dp = new int[n];
		Arrays.fill(dp, 0);
		dp[0] = 1;
		for (int j=0; j<Math.min(t,n); j++) {
			if (j==0) {
				for (int i=0; i<n; i++) {
					int best = 1;
					for (int k=0; k<i; k++) {
						if (nos[i]>=nos[k]) {
							best = Math.max(best, dp[k]+1);
						}
					}
					dp[i] = best;
				}
			} else {
				for (int i=0; i<n; i++) {
					int best = 1;
					for (int k=0; k<n; k++) {
						if (nos[i]>=nos[k]) {
							best = Math.max(best, dp[k]+1);
						}
					}
					dp[i] = best;
				}
			}
		}
		int ans = dp[0];
		for (int i=1; i<n; i++) ans = Math.max(ans, dp[i]);
		if (t>n){
			int[] freq = new int[300];
			for (int i=0; i<n; i++) {
				freq[nos[i]-1]++;
			}
			int mode = freq[0];
			for (int i=1; i<300; i++) {
				mode = Math.max(mode,freq[i]);
			}
			ans+=(t-n)*mode;
		}
		System.out.println(ans);
}


public class OnceAgain {
    public static void main(String[] args) {
        FastReader sc = new FastReader();
        final int maxAi = 3000;
        int n = sc.nextInt();
        int t = sc.nextInt();
        int[]ar = sc.readArray(n);

        int m = n * Math.min(n, t);
        int[] br = new int[m];
        for(int i = 0; i < m; ++i){
            br[i] = ar[i % n];
        }

        int lisLen = lenOfLis(br);

        int[] freq = new int[maxAi + 1];
        int maxFreq = 0;
        for(int a : ar){
            ++freq[a];
            maxFreq = Math.max(maxFreq, freq[a]);
        }
        //System.out.println(lisLen +", " + maxFreq);
        int ans = lisLen + (Math.max(0, t - m / n)) * maxFreq;
        System.out.println(ans);

    }

    private static int lenOfLis(int[] arr){
        int n = arr.length;
        int[] lis = new int[n];
        Arrays.fill(lis, 1);
        for(int i = 1; i < n; ++i){
            for(int j = 0; j < i; ++j){
                if(arr[j] <= arr[i])
                    lis[i] = Math.max(lis[i], 1 + lis[j]);
            }
        }
        int maxLen = 1;
        for(int len : lis)
            maxLen = Math.max(maxLen, len);

        return maxLen;
    }
    private static class FastReader {
        private BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }

        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }

        int nextInt() {
            return Integer.parseInt(next());
        }

        long nextLong() {
            return Long.parseLong(next());
        }

        double nextDouble() {
            return Double.parseDouble(next());
        }

        String nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readArray(int n){
            int[] arr = new int[n];
            for(int i = 0; i < n; ++i)
                arr[i] = nextInt();

            return arr;
        }
        long[] readLongArray(int n){
            long[] arr = new long[n];
            for(int i = 0; i < n; ++i)
                arr[i] = nextLong();

            return arr;
        }

        double[] readDoubleArray(int n){
            double[] arr = new double[n];
            for(int i = 0; i < n; ++i)
                arr[i] = nextDouble();
            return arr;
        }
    }
}


import java.io.*;
import java.util.*;

public class D {
	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int n = s.nextInt();
		int t = s.nextInt();
		int[] nos = new int[n];
		for (int i=0; i<n; i++) {
			nos[i] = s.nextInt();
		}
		int[] dp = new int[n];
		Arrays.fill(dp, 0);
		dp[0] = 1;
		for (int j=0; j<Math.min(t,n); j++) {
			if (j==0) {
				for (int i=0; i<n; i++) {
					int best = 1;
					for (int k=0; k<i; k++) {
						if (nos[i]>=nos[k]) {
							best = Math.max(best, dp[k]+1);
						}
					}
					dp[i] = best;
				}
			} else {
				for (int i=0; i<n; i++) {
					int best = 1;
					for (int k=0; k<n; k++) {
						if (nos[i]>=nos[k]) {
							best = Math.max(best, dp[k]+1);
						}
					}
					dp[i] = best;
				}
			}
		}
		int ans = dp[0];
		for (int i=1; i<n; i++) ans = Math.max(ans, dp[i]);
		if (t>n){
			int[] freq = new int[300];
			for (int i=0; i<n; i++) {
				freq[nos[i]-1]++;
			}
			int mode = freq[0];
			for (int i=1; i<300; i++) {
				mode = Math.max(mode,freq[i]);
			}
			ans+=(t-n)*mode;
		}
		System.out.println(ans);


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Feng Jiajie
 */
public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    OutputWriter out = new OutputWriter(outputStream);
    TaskD solver = new TaskD();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskD {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
      int n = in.readInt();
      int T = in.readInt();
      int[] num = IOUtils.readIntArray(in, n);
      int multi = Math.min(3, T);
      int[] numx = new int[multi * n];
      for (int i = 0; i < multi; i++) {
        for (int j = 0; j < n; j++) {
          numx[i * n + j] = num[j];
        }
      }
      int[] dp = new int[n * multi];
      Arrays.fill(dp, 0);
      for (int i = 0; i < n * multi; ++i) {
        int cnt = 0;
        for (int j = 0; j < i; j++) {
          if (numx[j] <= numx[i]) {
            cnt = Math.max(cnt, dp[j]);
          }
        }
        dp[i] = cnt + 1;
      }
      int ans = 0;
      if (T <= 3) {
        for (int i = 0; i < n * T; ++i) {
          ans = Math.max(ans, dp[i]);
        }
      } else {
        int[] cha = new int[n];
        for (int i = 0; i < n; i++) {
          cha[i] = dp[n * 2 + i] - dp[n + i];
        }
        for (int i = 0; i < n; i++) {
          ans = Math.max(ans, cha[i] * (T - 3) + dp[n * 2 + i]);
        }
      }
      out.printLine(ans);
    }

  }

  static class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
      writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer) {
      this.writer = new PrintWriter(writer);
    }

    public void close() {
      writer.close();
    }

    public void printLine(int i) {
      writer.println(i);
    }

  }

  static class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public InputReader(InputStream stream) {
      this.stream = stream;
    }

    public int read() {
      if (numChars == -1)
        throw new InputMismatchException();
      if (curChar >= numChars) {
        curChar = 0;
        try {
          numChars = stream.read(buf);
        } catch (IOException e) {
          throw new InputMismatchException();
        }
        if (numChars <= 0)
          return -1;
      }
      return buf[curChar++];
    }

    public int readInt() {
      int c = read();
      while (isSpaceChar(c))
        c = read();
      int sgn = 1;
      if (c == '-') {
        sgn = -1;
        c = read();
      }
      int res = 0;
      do {
        if (c < '0' || c > '9')
          throw new InputMismatchException();
        res *= 10;
        res += c - '0';
        c = read();
      } while (!isSpaceChar(c));
      return res * sgn;
    }

    public boolean isSpaceChar(int c) {
      if (filter != null)
        return filter.isSpaceChar(c);
      return isWhitespace(c);
    }

    public static boolean isWhitespace(int c) {
      return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public interface SpaceCharFilter {
      public boolean isSpaceChar(int ch);

    }

  }

  static class IOUtils {
    public static int[] readIntArray(InputReader in, int size) {
      int[] array = new int[size];
      for (int i = 0; i < size; i++)
        array[i] = in.readInt();
      return array;
    }

  }
}

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Feng Jiajie
 */
public class Main {
  public static void main(String[] args) {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    InputReader in = new InputReader(inputStream);
    OutputWriter out = new OutputWriter(outputStream);
    TaskD solver = new TaskD();
    solver.solve(1, in, out);
    out.close();
  }

  static class TaskD {
    public void solve(int testNumber, InputReader in, OutputWriter out) {
      int n = in.readInt();
      int T = in.readInt();
      int[] num = IOUtils.readIntArray(in, n);
      int multi = Math.max(3, T);
      int[] numx = new int[multi * n];
      for (int i = 0; i < multi; i++) {
        for (int j = 0; j < n; j++) {
          numx[i * n + j] = num[j];
        }
      }
      int[] dp = new int[n * multi];
      Arrays.fill(dp, 0);
      for (int i = 0; i < n * multi; ++i) {
        int cnt = 0;
        for (int j = 0; j < i; j++) {
          if (numx[j] <= numx[i]) {
            cnt = Math.max(cnt, dp[j]);
          }
        }
        dp[i] = cnt + 1;
      }
      int ans = 0;
      if (T <= 3) {
        for (int i = 0; i < n * T; ++i) {
          ans = Math.max(ans, dp[i]);
        }
      } else {
        int[] cha = new int[n];
        for (int i = 0; i < n; i++) {
          cha[i] = dp[n * 2 + i] - dp[n + i];
        }
        for (int i = 0; i < n; i++) {
          ans = Math.max(ans, cha[i] * (T - 3) + dp[n * 2 + i]);
        }
      }
      out.printLine(ans);
    }

  }

  static class OutputWriter {
    private final PrintWriter writer;

    public OutputWriter(OutputStream outputStream) {
      writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
    }

    public OutputWriter(Writer writer) {
      this.writer = new PrintWriter(writer);
    }

    public void close() {
      writer.close();
    }

    public void printLine(int i) {
      writer.println(i);
    }

  }

  static class InputReader {
    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public InputReader(InputStream stream) {
      this.stream = stream;
    }

    public int read() {
      if (numChars == -1)
        throw new InputMismatchException();
      if (curChar >= numChars) {
        curChar = 0;
        try {
          numChars = stream.read(buf);
        } catch (IOException e) {
          throw new InputMismatchException();
        }
        if (numChars <= 0)
          return -1;
      }
      return buf[curChar++];
    }

    public int readInt() {
      int c = read();
      while (isSpaceChar(c))
        c = read();
      int sgn = 1;
      if (c == '-') {
        sgn = -1;
        c = read();
      }
      int res = 0;
      do {
        if (c < '0' || c > '9')
          throw new InputMismatchException();
        res *= 10;
        res += c - '0';
        c = read();
      } while (!isSpaceChar(c));
      return res * sgn;
    }

    public boolean isSpaceChar(int c) {
      if (filter != null)
        return filter.isSpaceChar(c);
      return isWhitespace(c);
    }

    public static boolean isWhitespace(int c) {
      return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    public interface SpaceCharFilter {
      public boolean isSpaceChar(int ch);

    }

  }

  static class IOUtils {
    public static int[] readIntArray(InputReader in, int size) {
      int[] array = new int[size];
      for (int i = 0; i < size; i++)
        array[i] = in.readInt();
      return array;
    }

  }
}


//pakage joney_000[let_me_start]

import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
/******************** Main Class ***********************/
public class FastReader{

    private boolean finished = false;

    private InputStream stream;
    private byte[] buf = new byte[1024];
    private int curChar;
    private int numChars;
    private SpaceCharFilter filter;

    public FastReader(InputStream stream){
        this.stream = stream;
    }

    public int read(){
        if (numChars == -1){
            throw new InputMismatchException ();
        }
        if (curChar >= numChars){
            curChar = 0;
            try{
                numChars = stream.read (buf);
            } catch (IOException e){
                throw new InputMismatchException ();
            }
            if (numChars <= 0){
                return -1;
            }
        }
        return buf[curChar++];
    }

    public int peek(){
        if (numChars == -1){
            return -1;
        }
        if (curChar >= numChars){
            curChar = 0;
            try{
                numChars = stream.read (buf);
            } catch (IOException e){
                return -1;
            }
            if (numChars <= 0){
                return -1;
            }
        }
        return buf[curChar];
    }

    public int nextInt(){
        int c = read ();
        while (isSpaceChar (c))
            c = read ();
        int sgn = 1;
        if (c == '-'){
            sgn = -1;
            c = read ();
        }
        int res = 0;
        do{
            if(c==','){
                c = read();
            }
            if (c < '0' || c > '9'){
                throw new InputMismatchException ();
            }
            res *= 10;
            res += c - '0';
            c = read ();
        } while (!isSpaceChar (c));
        return res * sgn;
    }

    public long nextLong(){
        int c = read ();
        while (isSpaceChar (c))
            c = read ();
        int sgn = 1;
        if (c == '-'){
            sgn = -1;
            c = read ();
        }
        long res = 0;
        do{
            if (c < '0' || c > '9'){
                throw new InputMismatchException ();
            }
            res *= 10;
            res += c - '0';
            c = read ();
        } while (!isSpaceChar (c));
        return res * sgn;
    }

    public String nextString(){
        int c = read ();
        while (isSpaceChar (c))
            c = read ();
        StringBuilder res = new StringBuilder ();
        do{
            res.appendCodePoint (c);
            c = read ();
        } while (!isSpaceChar (c));
        return res.toString ();
    }

    public boolean isSpaceChar(int c){
        if (filter != null){
            return filter.isSpaceChar (c);
        }
        return isWhitespace (c);
    }

    public static boolean isWhitespace(int c){
        return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    }

    private String readLine0(){
        StringBuilder buf = new StringBuilder ();
        int c = read ();
        while (c != '\n' && c != -1){
            if (c != '\r'){
                buf.appendCodePoint (c);
            }
            c = read ();
        }
        return buf.toString ();
    }

    public String nextLine(){
        String s = readLine0 ();
        while (s.trim ().length () == 0)
            s = readLine0 ();
        return s;
    }

    public String nextLine(boolean ignoreEmptyLines){
        if (ignoreEmptyLines){
            return nextLine ();
        }else{
            return readLine0 ();
        }
    }

    public BigInteger nextBigInteger(){
        try{
            return new BigInteger (nextString ());
        } catch (NumberFormatException e){
            throw new InputMismatchException ();
        }
    }

    public char nextCharacter(){
        int c = read ();
        while (isSpaceChar (c))
            c = read ();
        return (char) c;
    }

    public double nextDouble(){
        int c = read ();
        while (isSpaceChar (c))
            c = read ();
        int sgn = 1;
        if (c == '-'){
            sgn = -1;
            c = read ();
        }
        double res = 0;
        while (!isSpaceChar (c) && c != '.'){
            if (c == 'e' || c == 'E'){
                return res * Math.pow (10, nextInt ());
            }
            if (c < '0' || c > '9'){
                throw new InputMismatchException ();
            }
            res *= 10;
            res += c - '0';
            c = read ();
        }
        if (c == '.'){
            c = read ();
            double m = 1;
            while (!isSpaceChar (c)){
                if (c == 'e' || c == 'E'){
                    return res * Math.pow (10, nextInt ());
                }
                if (c < '0' || c > '9'){
                    throw new InputMismatchException ();
                }
                m /= 10;
                res += (c - '0') * m;
                c = read ();
            }
        }
        return res * sgn;
    }

    public boolean isExhausted(){
        int value;
        while (isSpaceChar (value = peek ()) && value != -1)
            read ();
        return value == -1;
    }

    public String next(){
        return nextString ();
    }

    public SpaceCharFilter getFilter(){
        return filter;
    }

    public void setFilter(SpaceCharFilter filter){
        this.filter = filter;
    }

    public interface SpaceCharFilter{
        public boolean isSpaceChar(int ch);
    }
}
 /******************** Pair class ***********************/

 class Pair implements Comparable<Pair>{
 public int a;
 public int b;

 public Pair(){
  this.a = 0;
  this.b = 0;

 }
 public Pair(int a,int b , int n , long s){
  this.a = a;
  this.b = b;
 }
 public int compareTo(Pair p){
  if(this.a==p.a){
   return this.b-p.b;
  }
  return this.a-p.a;
 }
 public String toString(){
  return "a="+this.a+" b="+this.b;
 }

}

public class A
{
    public static InputStream inputStream = System.in;
    public static OutputStream outputStream = System.out;
    public static FastReader in = new FastReader(inputStream);;
    public static PrintWriter out = new PrintWriter(outputStream);;
    /*
    Overhead
    */
    public static int tempints[] = new int[100005];
    public static long templongs[] = new long[100005];
    public static double tempdoubles[] = new double[100005];
   //public static double tempdoubles[] = new double[11];
    public static char tempchars[] = new char[100005];

    public static void main(String[] args) throws java.lang.Exception
    {
        //let_me_start

        int a = i();int b = i();int p = i();int q = i();
        long v= 1;
        long x = 0;
        long y= 0;
        long l = 1;
        long r = 100000000000000000L;

        long z= 0;
        while (l <= r)
        {
         long m = (l + r) / 2;
         v=m;
         x= v-(v/p);
         y =v-v/q;
         z= v-v/p-v/q+v/(p*q);

          //z= v-(v/g);
        //If the search key on the left half
        if (x>a&&y>b&&x+y-z>a+b)
        {
            //Update right index
            r = m - 1;
        }
        //If the search key on the right half
        else if (x<a||y<b||x+y-z<a+b)
        {
            //Update the left index
            l = m + 1;
        }
        //We found the key
        else
        {
            v= m;
            break;
        }
    }
        out.write(""+v);
          out.flush();

}

//****************************** Utilities ***********************//
 public static boolean isPrime(long n)throws Exception{
  if(n==1)return false;
  if(n<=3)return true;
  if(n%2==0)return false;
  for(int i=2 ;i <= Math.sqrt(n); i++){
   if(n%i==0)return false;
  }
  return true;
 }
 // sieve
 public static int[] primes(int n)throws Exception{       // for(int i=1;i<=arr.length-1;i++)out.write(""+arr[i]+" ");
  boolean arr[] = new boolean[n+1];
  Arrays.fill(arr,true);
  for(int i=1;i<=Math.sqrt(n);i++){
    if(!arr[i])continue;
    for(int j = 2*i ;j<=n;j+=i){
        arr[i]=false;
    }
  }
  LinkedList<Integer> ll = new LinkedList<Integer>();
  for(int i=1;i<=n;i++){
   if(arr[i])ll.add(i);
  }
  n = ll.size();

  int primes[] = new int[n+1];
  for(int i=1;i<=n;i++){
    primes[i]=ll.removeFirst();
  }
  return primes;
 }
 public static long gcd (long a , long b)throws Exception{
  if(b==0)return a;
  return gcd(b , a%b);
 }
 public static long lcm (long a , long b)throws Exception{
  if(a==0||b==0)return 0;
  return (a*b)/gcd(a,b);
 }
 public static long mulmod(long a , long b ,long mod)throws Exception{
   if(a==0||b==0)return 0;
   if(b==1)return a;
   long ans = mulmod(a,b/2,mod);
   ans = (ans*2)% mod;
   if(b%2==1)ans = (a + ans)% mod;
   return ans;
 }
 public static long pow(long a , long b ,long mod)throws Exception{
   if(b==0)return 1;
   if(b==1)return a;
   long ans = pow(a,b/2,mod);
   ans = (ans * ans)% mod;
   if(b%2==1)ans = (a * ans)% mod;
   return ans;
 }
 // 20*20   nCr Pascal Table
 public static long[][] ncrTable()throws Exception{
  long ncr[][] = new long[21][21];
  for(int i=0 ;i<=20 ;i++){ncr[i][0]=1;ncr[i][i]=1;}
  for(int j=0;j<=20 ;j++){
   for(int i=j+1;i<= 20 ;i++){
    ncr[i][j] = ncr[i-1][j]+ncr[i-1][j-1];
   }
  }
  return ncr;
 }
//*******************************I/O******************************//
public static int i()throws Exception{
 //return Integer.parseInt(br.readLine().trim());
 return in.nextInt();
}
public static int[] is(int n)throws Exception{
  //int arr[] = new int[n+1];
  for(int i=1 ; i <= n ;i++)tempints[i] = in.nextInt();
 return tempints;
}
public static long l()throws Exception{
 return in.nextLong();
}
public static long[] ls(int n)throws Exception{
  //long arr[] = new long[n+1];
  for(int i=1 ; i <= n ;i++)templongs[i] = in.nextLong();
 return templongs;
}

public static double d()throws Exception{
 return in.nextDouble();
}
public static double[] ds(int n)throws Exception{
  //double arr[] = new double[n+1];
  for(int i=1 ; i <= n ;i++)tempdoubles[i] = in.nextDouble();
 return tempdoubles;
}
public static char c()throws Exception{
 return in.nextCharacter();
}
public static char[] cs(int n)throws Exception{
  //double arr[] = new double[n+1];
  for(int i=1 ; i <= n ;i++)tempchars[i] = in.nextCharacter();
 return tempchars;
}
public static String s()throws Exception{
 return in.nextLine();
}
public static BigInteger bi()throws Exception{
 return in.nextBigInteger();
}
//***********************I/O ENDS ***********************//
//*********************** 0.3%f [precision]***********************//
/* roundoff upto 2 digits
   double roundOff = Math.round(a * 100.0) / 100.0;
                    or
   System.out.printf("%.2f", val);

*/
/* print upto 2 digits after decimal
  val = ((long)(val * 100.0))/100.0;

*/
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.StringTokenizer;

/*
 public class _482A{ }
 */

public class _483B
{
	public void solve() throws FileNotFoundException
	{
		InputStream inputStream = System.in;

		InputHelper inputHelper = new InputHelper(inputStream);

		PrintStream out = System.out;

		// actual solution

		int c1 = inputHelper.readInteger();
		int c2 = inputHelper.readInteger();

		int x = inputHelper.readInteger();
		int y = inputHelper.readInteger();

		long min = 2;
		long max = 4000000000L;

		long ans = max;

		while (min <= max)
		{
			long mid = (min + max) / 2;

			if (isV(mid, c1, c2, x, y))
			{
				ans = Math.min(ans, mid);
				max = mid - 1;
			}
			else
			{
				min = mid + 1;
			}
		}

		out.println(ans);
		// end here

	}

	private boolean isV(long mid, int c1, int c2, int x, int y)
	{
		long xyl = lcm(x, y);

		long xq = mid / x;
		long yq = mid / y;

		long xyq = mid / xyl;

		long cc1 = yq - xyq;
		long cc2 = xq - xyq;

		long rn = mid - xq - yq + xyq;

		if (Math.max((c1 - cc1), 0L) + Math.max((c2 - cc2), 0L) <= rn)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	private long lcm(int x, int y)
	{
		return (x * y) / gcd(x, y);
	}

	private long gcd(long a, long b)
	{
		if (b == 0)
		{
			return a;
		}
		else
		{
			return gcd(b, a % b);
		}
	}

	public static void main(String[] args) throws FileNotFoundException
	{
		(new _483B()).solve();
	}

	class InputHelper
	{
		StringTokenizer tokenizer = null;
		private BufferedReader bufferedReader;

		public InputHelper(InputStream inputStream)
		{
			InputStreamReader inputStreamReader = new InputStreamReader(
					inputStream);
			bufferedReader = new BufferedReader(inputStreamReader, 16384);
		}

		public String read()
		{
			while (tokenizer == null || !tokenizer.hasMoreTokens())
			{
				try
				{
					String line = bufferedReader.readLine();
					if (line == null)
					{
						return null;
					}
					tokenizer = new StringTokenizer(line);
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}

			return tokenizer.nextToken();
		}

		public Integer readInteger()
		{
			return Integer.parseInt(read());
		}

		public Long readLong()
		{
			return Long.parseLong(read());
		}
	}
}


import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.StringTokenizer;

/*
 public class _482A{ }
 */

public class _483A
{
	public void solve() throws FileNotFoundException
	{
		InputStream inputStream = System.in;

		InputHelper inputHelper = new InputHelper(inputStream);

		PrintStream out = System.out;

		// actual solution

		int c1 = inputHelper.readInteger();
		int c2 = inputHelper.readInteger();

		int x = inputHelper.readInteger();
		int y = inputHelper.readInteger();

		long min = 2;
		long max = 4000000000L;

		long ans = max;

		while (min <= max)
		{
			long mid = (min + max) / 2;

			if (isV(mid, c1, c2, x, y))
			{
				ans = Math.min(ans, mid);
				max = mid - 1;
			}
			else
			{
				min = mid + 1;
			}
		}

		out.println(ans);
		// end here

	}

	private boolean isV(long mid, int c1, int c2, int x, int y)
	{
		long xyl = lcm(x, y);

		long xq = mid / x;
		long yq = mid / y;

		long xyq = mid / xyl;

		long cc1 = yq - xyq;
		long cc2 = xq - xyq;

		long rn = mid - xq - yq + xyq;

		if (Math.max((c1 - cc1), 0L) + Math.max((c2 - cc2), 0L) <= rn)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	private long lcm(int x, int y)
	{
		return (x * y) / gcd(x, y);
	}

	private long gcd(long a, long b)
	{
		if (b == 0)
		{
			return a;
		}
		else
		{
			return gcd(b, a % b);
		}
	}

	public static void main(String[] args) throws FileNotFoundException
	{
		(new _483A()).solve();
	}

	class InputHelper
	{
		StringTokenizer tokenizer = null;
		private BufferedReader bufferedReader;

		public InputHelper(InputStream inputStream)
		{
			InputStreamReader inputStreamReader = new InputStreamReader(
					inputStream);
			bufferedReader = new BufferedReader(inputStreamReader, 16384);
		}

		public String read()
		{
			while (tokenizer == null || !tokenizer.hasMoreTokens())
			{
				try
				{
					String line = bufferedReader.readLine();
					if (line == null)
					{
						return null;
					}
					tokenizer = new StringTokenizer(line);
				}
				catch (IOException e)
				{
					e.printStackTrace();
				}
			}

			return tokenizer.nextToken();
		}

		public Integer readInteger()
		{
			return Integer.parseInt(read());
		}

		public Long readLong()
		{
			return Long.parseLong(read());
		}
	}
}


import java.util.*;
import java.math.*;
import java.io.*;
import java.awt.Point;

import com.sun.xml.internal.fastinfoset.algorithm.BuiltInEncodingAlgorithm.WordListener;

import static java.util.Arrays.*;
import static java.lang.Integer.*;
import static java.lang.Double.*;
import static java.lang.Long.*;
import static java.lang.Short.*;
import static java.lang.Math.*;
import static java.math.BigInteger.*;
import static java.util.Collections.*;

public class B {
	static class Vertex {

	}

	static class Graph {

	}

	public static void main(String[] xa) throws IOException {
		Scanner sc = new Scanner(new BufferedInputStream(System.in));

		long c1 = sc.nextLong();
		long c2 = sc.nextLong();

		int x = sc.nextInt();
		int y = sc.nextInt();

		long left = c1 + c2 - 1;
		long right = Long.MAX_VALUE;
		long ans = Long.MAX_VALUE;
		while (true) {
			long mid = left + (right - left) / 2;
			boolean worksAtMiddle = worksOut(c1, x, c2, y, mid);
			boolean foundAnswer = worksOut(c1, x, c2, y, mid)
					&& !worksOut(c1, x, c2, y, mid - 1);
//			System.out.println(worksAtMiddle + " " + mid);
//			System.out.println("f: " + foundAnswer);
			if(foundAnswer) {
				System.out.println(mid);
				break;
			}

			if (worksAtMiddle) {
				ans = Math.min(ans, mid);
				right = mid - 1;
			} else if (!worksAtMiddle) {
				left = mid + 1;
			}
//			System.out.println(left +" " + right);
		}

	}

	static boolean worksOut(long c1, int x, long c2, int y, long guess) {
		long numbersForOne = guess - guess / x;
		long numbersForTwo = guess - guess / y;

		// case one
		long numbersForOneIfOneAdjusts = numbersForOne - (guess / (x * y));
		long numbersForTwoIfTwoAdjusts = numbersForTwo - (guess / (x * y));

		return (numbersForOneIfOneAdjusts >= c1 && numbersForTwo >= c2)
				|| (numbersForOne >= c1 && numbersForTwoIfTwoAdjusts >= c2);
	}
}


public class Main {

    public static void main(String[] args) {
    String S="";
    double s=3;
    int X = s/s;
    }

}


import java.util.Scanner;
public class ProblemA {
    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        kb.nextLine();
        String[] x = kb.nextLine().split(" ");
        String[] y = kb.nextLine().split(" ");
        int[] xs = new int[x.length()];
        int[] ys = new int[y.length()];
        int sumx = 0;
        int sumy = 0;
        for (int i = 0; i < x.length(); i++) {
            xs[i] = Integer.parseInt(x[i]);
            ys[i] = Integer.parseInt(y[i]);
            sumx += xs[i];
            sumy += ys[i];
        }
        if (sumx >= sumy) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}


import java.io.*;
import java.util.StringTokenizer;
import java.util.TreeMap;

/**
 * Created by timur on 28.03.15.
 */

public class TaskA {
    boolean eof;
    BufferedReader br;
    StringTokenizer st;
    PrintWriter out;

    public static void main(String[] args) throws IOException {
        new TaskA().run();
    }

    public String nextToken() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (Exception e) {
                eof = true;
                return "-1";
            }
        }
        return st.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(nextToken());
    }

    public long nextLong() {
        return Long.parseLong(nextToken());
    }

    double nextDouble() {
        return Double.parseDouble(nextToken());
    }

    String nextLine() throws IOException {
        return br.readLine();
    }

    void run() throws IOException {
        InputStream input = System.in;
        PrintStream output = System.out;
        try {
            File f = new File("a.in");
            if (f.exists() && f.canRead()) {
                input = new FileInputStream(f);
                output = new PrintStream("a.out");
            }
        } catch (Throwable e) {
        }
        br = new BufferedReader(new InputStreamReader(input));
        out = new PrintWriter(output);
        solve();
        br.close();
        out.close();
    }

    void solve() {
        int n = nextInt();
        int x = nextInt();
        TreeMap<Integer, Integer> map = new TreeMap<>();
        for (int i = 0; i < n; i++)
    }
}


import java.util.*;

public class susbus{
 public static void main(String[] args){
  Scanner sc = new Scanner(System.in);
  int n = sc.nextInt();
  int [] stonesOne = new int[n];
  int [] stonesTwo = new int[n];
  int [] stonesOne = {1,2,3,4,5};
  int [] stonesTwo = {2,1,4,3,5};
  for (int i=0;i<n;i++) {
	  stonesOne[i] = sc.nextInt();
  }
 int sum1=0;
 for (int i=0;i<n;i++) {
	 sum1+=stonesOne[i];
 }
 int sum2=0;
 for (int i=0;i<n;i++) {
	 sum2+=stonesTwo[i];
 }
 if ((sum1-sum2) <= n && (sum1-sum2) >=0) {
	 System.out.println("Yes");
 }
 else {
	 System.out.println("No");

 }
}
}


import java.io.*;
import java.util.*;

public class A implements Runnable{
	StringTokenizer st;
	BufferedReader in;
	PrintWriter out;

	public final String template = "template";

	public static void main(String[] args) {
		new Thread(new A()).start();
	}

	public void run() {
		try {
			//in = new BufferedReader(new FileReader(template + ".in"));
			//out = new PrintWriter(new FileWriter(template + ".out"));
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(new OutputStreamWriter(System.out));
			solve();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(9000);
		} finally {
			out.flush();
			out.close();
		}
	}

	String nextToken() throws IOException {
		while(st == null || !st.hasMoreTokens()) {
			st = new StringTokenizer(in.readLine());
		}
		return st.nextToken();
	}

	int nextInt() throws NumberFormatException, IOException {
		return Integer.parseInt(nextToken());
	}

	long nextLong() throws NumberFormatException, IOException {
		return Long.parseLong(nextToken());
	}

	double nextDouble() throws NumberFormatException, IOException {
		return Double.parseDouble(nextToken());
	}

	void solve() throws NumberFormatException, IOException {
		int w = 0, b = 0;
		for (int i = 0; i < 8; i++) {
			String s = nextToken();
			for (int j = 0; j < 8; j++){
				if (s.charAt(j) == 'W') w++;
				else b++;
			}
		}
		if (w == b) out.print("YES"); else out.print("NO");
	}

}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class A {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
		for (int i = 0; i < 8; i++) {
			String m = rd.readLine();
			int w = 0;
			for (int j = 0; j < 8; j++) {
				if (m.charAt(j) == 'W')
					w++;
			}
			if (w != 4) {
				System.out.println("NO");
				System.exit(0);
			}
		}
		System.out.println("YES");

	}

}


import java.io.*;
import java.util.*;
import java.math.*;

public class P259A {

  @SuppressWarnings("unchecked")
  public void run() throws Exception {
    boolean ok = true;
    for (int i = 0; i < 8; i++) {
      String s = nextLine();
      int b = 0;
      int w = 0;

      for (char c : s.toCharArray()) {
        if (c == 'B') {
          b++;
        } else {
          w++;
        }
      }

      ok &= (b == 4 && w == 4);
    }

    println((ok) ? "YES" : "NO");
  }

  public static void main(String... args) throws Exception {
    br = new BufferedReader(new InputStreamReader(System.in));
    pw = new PrintWriter(new BufferedOutputStream(System.out));
    new P259A().run();
    br.close();
    pw.close();
  }

  static BufferedReader br;
  static PrintWriter pw;
  StringTokenizer stok;

  String nextToken() throws IOException {
    while (stok == null || !stok.hasMoreTokens()) {
      String s = br.readLine();
      if (s == null) { return null; }
      stok = new StringTokenizer(s);
    }
    return stok.nextToken();
  }

  void print(byte b) { print("" + b); }
  void print(int i) { print("" + i); }
  void print(long l) { print("" + l); }
  void print(double d) { print("" + d); }
  void print(char c) { print("" + c); }
  void print(Object o) {
    if (o instanceof int[]) { print(Arrays.toString((int [])o));
    } else if (o instanceof long[]) { print(Arrays.toString((long [])o));
    } else if (o instanceof char[]) { print(Arrays.toString((char [])o));
    } else if (o instanceof byte[]) { print(Arrays.toString((byte [])o));
    } else if (o instanceof short[]) { print(Arrays.toString((short [])o));
    } else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o));
    } else if (o instanceof float[]) { print(Arrays.toString((float [])o));
    } else if (o instanceof double[]) { print(Arrays.toString((double [])o));
    } else if (o instanceof Object[]) { print(Arrays.toString((Object [])o));
    } else { print("" + o); }
  }
  void print(String s) { pw.print(s); }
  void println() { println(""); }
  void println(byte b) { println("" + b); }
  void println(int i) { println("" + i); }
  void println(long l) { println("" + l); }
  void println(double d) { println("" + d); }
  void println(char c) { println("" + c); }
  void println(Object o) { print(o); println(""); }
  void println(String s) { pw.println(s); }
  int nextInt() throws IOException { return Integer.parseInt(nextToken()); }
  long nextLong() throws IOException { return Long.parseLong(nextToken()); }
  double nextDouble() throws IOException { return Double.parseDouble(nextToken()); }
  char nextChar() throws IOException { return (char) (br.read()); }
  String next() throws IOException { return nextToken(); }
  String nextLine() throws IOException { return br.readLine(); }
  int [] readInt(int size) throws IOException {
    int [] array = new int [size];
    for (int i = 0; i < size; i++) { array[i] = nextInt(); }
    return array;
  }
  long [] readLong(int size) throws IOException {
    long [] array = new long [size];
    for (int i = 0; i < size; i++) { array[i] = nextLong(); }
    return array;
  }
  double [] readDouble(int size) throws IOException {
    double [] array = new double [size];
    for (int i = 0; i < size; i++) { array[i] = nextDouble(); }
    return array;
  }
  String [] readLines(int size) throws IOException {
    String [] array = new String [size];
    for (int i = 0; i < size; i++) { array[i] = nextLine(); }
    return array;
  }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class A157 {

    public static void main(String [] args) throws IOException {

         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

         String [] board = new String[8];

         for(int i=0; i<8; i++) {
             board[i] = br.readLine();
             int w=0;
             int b=0;
             for(int j=0; j<8; j++)
                 if(board[i].charAt(j) == 'W')
                     w++;
                 else
                     b++;
             //System.out.println(b);
             if(b!=4 || w!=4){
                 System.out.println("NO");
                 return;
             }
         }
         System.out.println("YES");

    }
}


import java.util.*;
import java.math.BigInteger;
public class Main {
    public static void main(String[] args)
    {
        BigInteger x,k,a,b,md,to,on;
        md = BigInteger.valueOf(1000000007);
        to = BigInteger.valueOf(2);
        on = new BigInteger.ONE;
	Scanner inp = new Scanner(System.in);
	x = inp.nextBigInteger();
	k = inp.nextBigInteger();
	if(x.compareTo(BigInteger.ZERO)==0) System.out.println(0);
	else{
	    a = to.modPow(k.add(on),md);
        a = a.multiply(x);
        b = to.modPow(k,md);
        System.out.println(a.subtract(b).add(on).mod(md));
	}

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import java.math.*;

public class Wardrobe {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        //int t = Integer.parseInt(in.readLine());
        BigInteger MOD = new BigInteger("1000000007");
        long x,k;
        String[] inp = in.readLine().split(" ");
        x = Long.parseLong(inp[0]);
        k = Long.parseLong(inp[1]);
        if(k==0){
            BigInteger res = BigInteger.valueOf(x).multiply(BigInteger.TWO);
        	System.out.println(res.mod(MOD));
        	return;
        }
        if(x==0){
        	System.out.println(0);
        	return;
        }
        BigInteger bi = BigInteger.valueOf(2*x-1);
        BigInteger TWO = BigInteger.valueOf(2);
        TWO = TWO.modPow(BigInteger.valueOf(k),MOD);
        bi = bi.multiply(TWO).add(BigInteger.ONE);
        System.out.println(bi.mod(MOD));
    }
}


import java.io.*;
import java.util.*;
import java.math.*;

public class C
{

    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        BigInteger x = sc.nextBigInteger();
        BigInteger k = sc.nextBigInteger();
        BigInteger zero = new BigInteger("0");
        BigInteger one = new BigInteger("1");
        BigInteger two = new BigInteger("2");
        BigInteger b = (k.add(one)).mod(modulo);
        BigInteger a = k.mod(modulo);
        BigInteger modulo = new BigInteger("1000000006");
        BigInteger ans = two.modPow(b,modulo);
        ans = ans.multiply(x);
        ans = ans.subtract(two.modPow(a,modulo));
        ans = ans.add(one);
        ans = ans.mod(modulo);
        if (x.equals(zero))
        {
         System.out.println(0);
         }
         else
         {
        System.out.println(ans);
        }
    }
}


import java.io.*;
import java.util.*;

public class C {

	static long x, y,mod = 1000*1000*1000+7;;
    static long gcd(long a ,long b)
    {
        if(b==0)
            return a;
        return gcd(b ,a%b);
    }
    public static int gcd(int a, int b) {
        while (b > 0) {
            int c = a;
            a = b;
            b = c % b;
        }
        return a;
    }
    static long expo(long x ,long n ,long mod)
    {
        long res=1;
        while(n>0)
        {
            if(n%2==1)
                res=(res*x)%mod;
            x=(x*x)%mod;
            n=n/2;
        }
        return res%mod;
    }

    static long fermat(long a ,long m)
    {
        return expo(a,m-2,m)%m;
    }

    static void extendedEuclidean(long a ,long m)
    {
        if(m==0)
        {
            x=1;
            y=0;
        }
        else
        {
            extendedEuclidean(m,a%m);
            long temp=x;
            x=y;
            y=temp-(a/m)*y;
        }

    }

    static long EuclidInverse(long a ,long m)
    {
        extendedEuclidean(a ,m);
        return (x%m+m)%m;
    }

	public static void main(String[] args) {

		FastScanner s = new FastScanner();
		long x = s.nextLong();
		long k = s.nextLong();

		if(k==0)
		{
			long ans = (2*x)%mod;
			System.out.println(ans);
			return;
		}
		if(x==0)
		{
			System.out.println(0);
			return;
		}

		long a = (expo(2,k+1,mod)*x - expo(2,k,mod)+1)%mod

		System.out.println(a);
	}
static class FastScanner {

	    private InputStream mIs;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public FastScanner() {
			this(System.in);
		}

		public FastScanner(InputStream is) {
			mIs = is;
		}

		public int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = mIs.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public String nextLine() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndOfLine(c));
			return res.toString();
		}

		public String nextString() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public long nextLong() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			long res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public int nextInt() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-') {
				sgn = -1;
				c = read();
			}
			int res = 0;
			do {
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			} while (!isSpaceChar(c));
			return res * sgn;
		}

		public boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public boolean isEndOfLine(int c) {
			return c == '\n' || c == '\r' || c == -1;

		}

	}

}


import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;

public class Test {

  static PrintWriter writer =
      new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));

  static int readInt() {
    int ans = 0;
    boolean neg = false;
    try {
      boolean start = false;
      for (int c = 0; (c = System.in.read()) != -1; ) {
        if (c == '-') {
          start = true;
          neg = true;
          continue;
        } else if (c >= '0' && c <= '9') {
          start = true;
          ans = ans * 10 + c - '0';
        } else if (start) break;
      }
    } catch (IOException e) {
    }
    return neg ? -ans : ans;
  }

  static long readLong() {
    long ans = 0;
    boolean neg = false;
    try {
      boolean start = false;
      for (int c = 0; (c = System.in.read()) != -1; ) {
        if (c == '-') {
          start = true;
          neg = true;
          continue;
        } else if (c >= '0' && c <= '9') {
          start = true;
          ans = ans * 10 + c - '0';
        } else if (start) break;
      }
    } catch (IOException e) {
    }
    return neg ? -ans : ans;
  }

  static String readString() {
    StringBuilder b = new StringBuilder();
    try {
      boolean start = false;
      for (int c = 0; (c = System.in.read()) != -1; ) {
        if (Character.isLetterOrDigit(c)) {
          start = true;
          b.append((char) c);
        } else if (start) break;
      }
    } catch (IOException e) {
    }
    return b.toString();
  }

  static int readChars(char[] a, int off) {
    int cnt = 0;
    try {
      boolean start = false;
      for (int c = 0; (c = System.in.read()) != -1; ) {
        if (Character.isLetterOrDigit(c)) {
          start = true;
          a[off + cnt++] = (char) c;
        } else if (start) break;
      }
    } catch (IOException e) {
    }
    return cnt;
  }

  public static void main(String[] args) {
    Test te = new Test();
    te.start();
    writer.flush();
  }

  long f(long n) {
    long ret = n;
    long v = n;
    for (long i = 2; i*i <= v; i++)
      if (v % i == 0) {
        while (v % i == 0) v /= i;
        ret =ret*(i-1)/i;
      }
    if (v > 1) ret = ret*(v-1)/v;
    return ret;
  }

  long F(long n, long k) {
    if (n == 1) return 1;
    if (k == 1) return f(n);
    if (k % 2 == 0) return F(n, k-1);
    return f(F(n, k-1));
  }

  void start() {
    long n = readLong(), k = readLong();
    writer.println(F(n, k) % 1_000_000_007);
  }
}