Problem:
Score : 1000 points

Problem Statement

Rng is baking cookies.

Initially, he can bake one cookie per second.

He can also eat the cookies baked by himself.
When there are x cookies not yet eaten, he can choose to eat all those cookies. After he finishes eating those cookies, the number of cookies he can bake per second becomes x. Note that a cookie always needs to be baked for 1 second, that is, he cannot bake a cookie in 1/x seconds when x > 1.
When he choose to eat the cookies, he must eat all of them; he cannot choose to eat only part of them.
It takes him A seconds to eat the cookies regardless of how many, during which no cookies can be baked.

He wants to give N cookies to Grandma.
Find the shortest time needed to produce at least N cookies not yet eaten.

Constraints

1≦N≦10^{12}

0≦A≦10^{12}

A is an integer.

Partial Score

500 points will be awarded for passing the test set satisfying N≦10^6 and A≦10^6.

Additional 500 points will be awarded for passing the test set without additional constraints.

Input

The input is given from Standard Input in the following format:

N A

Output

Print the shortest time needed to produce at least N cookies not yet eaten.

Sample Input 1

8 1

Sample Output 1

7

It is possible to produce 8 cookies in 7 seconds, as follows:

After 1 second: 1 cookie is done.

After 2 seconds: 1 more cookie is done, totaling 2. Now, Rng starts eating those 2 cookies.

After 3 seconds: He finishes eating the cookies, and he can now bake 2 cookies per second.

After 4 seconds: 2 cookies are done.

After 5 seconds: 2 more cookies are done, totaling 4.

After 6 seconds: 2 more cookies are done, totaling 6.

After 7 seconds: 2 more cookies are done, totaling 8.

Sample Input 2

1000000000000 1000000000000

Sample Output 2

1000000000000

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
import java.util.StringTokenizer;

public class Main {
	
	public static long calc_time(long A, long N, long curr_time, long curr_cokkie, long can_bake){
		return curr_time + Math.max(0, ((N - curr_cokkie) + (can_bake - 1))) / can_bake;
	}
	
	public static void main(String[] args){
		try(Scanner sc = new Scanner(System.in)){
			
			final long BIG_N = sc.nextLong();
			final long BIG_A = sc.nextLong();
			
			assert(BIG_N <= 1000000 && BIG_A <= 1000000);
			
			final int N = (int)(BIG_N);
			final int A = (int)(BIG_A);
			
			long[] bake_time = new long[2 * N + 1];
			long[] next_time = new long[2 * N + 1];
			
			Arrays.fill(bake_time, Long.MAX_VALUE / 2);
			bake_time[1] = 1;
			next_time[0] = bake_time[0] = 0;
			while(true){
				boolean updated = false;
				
				System.arraycopy(bake_time, 1, next_time, 1, bake_time.length - 1);
				
				for(int curr = 1; curr < N; curr++){
					if(next_time[curr + 1] > bake_time[curr] + 1){
						updated = true;
						next_time[curr + 1] = bake_time[curr] + 1;
					}
				}
				for(int curr = 1; curr <= (N + curr) / 2; curr++){
					int count = 1;
					
					for(int next = curr * 2; next <= N + curr; next += curr){
						if(next_time[next] > bake_time[curr] + A + (count + 1)){
							updated = true;
							next_time[next] = bake_time[curr] + A + (count + 1);
						}
						
						count++;
					}
				}
				
				/*
				for(int i = 0; i < 30; i++){
					System.out.print(bake_time[i] + " ");
				}
				*/
				//System.out.println(bake_time[N]);
				
				if(!updated){ break; }
				
				{
					long[] tmp = bake_time;
					bake_time = next_time;
					next_time = tmp;
				}
			}
			
			long min = Long.MAX_VALUE;
			for(int i = N; i < bake_time.length; i++){
				min = Math.min(min, bake_time[i]);
			}
			System.out.println(min);
		}
	}
	
	public static class Scanner implements AutoCloseable {
		private BufferedReader br;
		private StringTokenizer tok;

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

		private void getLine() {
			try {
				while (!hasNext()) {tok = new StringTokenizer(br.readLine());}
			} catch(IOException e){ /* ignore */ }
		}

		private boolean hasNext() {
			return tok != null && tok.hasMoreTokens();
		}

		public String next() {
			getLine(); return tok.nextToken();
		}

		public int nextInt(){
			return Integer.parseInt(next());
		}
		
		public long nextLong(){
			return Long.parseLong(next());
		}
		// 他のnextXXXもXXX.parseXXX()メソッドを使って作れるので省略

		public void close() {
			try{ br.close(); } catch (IOException e){ /*ignore*/ }
		}
	}
}