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

public class CF139D {
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		char[] cc = br.readLine().toCharArray();
		int[] aa = new int[10];
		for (int i = 0; i < cc.length; i++) {
			int d = cc[i] - '0';
			aa[d]++;
		}
		int[] a0 = new int[10];
		int[] a1 = new int[10];
		int[] a2 = new int[10];
		int max = 0, z_ = 0, x_ = 0;
		for (int z = 0; z <= aa[0]; z++) {
			for (int d = 0; d <= 9; d++)
				a0[d] = aa[d];
			a0[0] -= z;
			for (int x = 1; x <= 5; x++)
				if (a0[x] > 0 && a0[10 - x] > 0) {
					for (int d = 0; d <= 9; d++)
						a1[d] = a2[d] = a0[d];
					a1[x]--;
					a2[10 - x]--;
					int cnt = 0;
					for (int d = 0; d <= 9; d++)
						cnt += Math.min(a1[d], a2[9 - d]);
					if (max < cnt + 1 + z) {
						max = cnt + 1 + z;
						z_ = z;
						x_ = x;
					}
				}
		}
		StringBuilder s1 = new StringBuilder();
		StringBuilder s2 = new StringBuilder();
		for (int d = 0; d <= 9; d++)
			a0[d] = aa[d];
		a0[0] -= z_;
		for (int i = 0; i < z_; i++) {
			s1.append('0');
			s2.append('0');
		}
		for (int d = 0; d <= 9; d++)
			a1[d] = a2[d] = a0[d];
		a1[x_]--;
		a2[10 - x_]--;
		s1.append(x_);
		s2.append(10 - x_);
		for (int d = 0; d <= 9; d++)
			while (a1[d] > 0 && a2[9 - d] > 0) {
				a1[d]--;
				a2[9 - d]--;
				s1.append(d);
				s2.append(9 - d);
			}
		for (int d = 0; d <= 9; d++) {
			for (int i = 0; i < a1[d]; i++)
				s1.append(d);
			for (int i = 0; i < a2[d]; i++)
				s2.append(d);
		}
		System.out.println(s1.reverse());
		System.out.println(s2.reverse());
	}
}


import sun.plugin.javascript.navig.Array;

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

public class B {
	static Scanner in; static int next() throws Exception {return in.nextInt();};
//	static StreamTokenizer in; static long next() throws Exception {in.nextToken(); return (long) in.nval;}
//	static BufferedReader in;
	static PrintWriter out;

	public static void main(String[] args) throws Exception {
		in = new Scanner(System.in);
//		in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
//		in = new BufferedReader(new InputStreamReader(System.in));
		out = new PrintWriter(System.out);

        String s = new String(in.next());

        int a[] = new int[10];
        Arrays.fill(a,0);
        int n = s.length();
        for (int i = 0;i < n;i++) a[s.charAt(i)-'0']++;
        int res = 0, best = 0;
        for (int i = 1;i <=5;i++) {
            int counter = 0;
            int b[] = a.clone();
            int c[] = a.clone();
            if (b[i] > 0 && c[10-i] > 0) {
                b[i]--;
                c[10-i]--;
                counter++;
                for (int j = 0;j < 10;j++) {
                    counter += Math.min(b[j],c[9-j]);
                    b[j] -= Math.min(b[j],c[9-j]);
                    c[9-j] -= Math.min(b[j],c[9-j]);
                }
            }
            counter += Math.min(b[0], c[0]);
            if (counter > res) {
                res = counter;
                best = i;
            }
        }

        int pairA[] = new int[n];
        int pairB[] = new int[n];
        int z = n-1;

        int b[] = a.clone();
        int c[] = a.clone();
        if (b[best] > 0 && c[10-best] > 0) {
            b[best]--;
            c[10-best]--;
            pairA[z] = best;
            pairB[z] = 10-best;
            z--;
            for (int j = 0;j < 10;j++) {
                for (int u = 0;b[j]>0&&c[9-j]>0;u++){
                    pairA[z] = j;
                    b[j]--;
                    pairB[z] = 9-j;
                    c[9-j]--;
                    z--;
                }
            }
            int sum1 = 0;
            for (int i = 1;i < 10;i++) {
                sum1 += b[i];
            }
            int sum2 = 0;

            for (int i = 1;i < 10;i++) {
                sum2+=c[i];
            }
            for (int i = 1;i < 10;i++) {
                while (b[i] > 0) {
                    out.print(i);
                    b[i]--;
                }
            }
            if (sum1 < sum2) {
                for (int i = sum1;i < sum2;i++)
                    c[0]--;
                out.print(0);
            }
            for (int i = z+1;i < n;i++) out.print(pairA[i]);
            for (int i = 0;i < b[0];i++) out.print(0);
            out.println();

            for (int i = 1;i < 10;i++) {
                while (c[i] > 0) {
                    out.print(i);
                    c[i]--;
                }
            }

            if (sum1 > sum2) {
                for (int i = sum2;i < sum1;i++)
                    b[0]--;
                out.print(0);
            }
            for (int i = z+1;i < n;i++) out.print(pairB[i]);
            for (int i = 0;i < c[0];i++) out.print(0);
            out.println();
        } else {
            char g[] = s.toCharArray();
            Arrays.sort(g);
            for (int i = n-1;i >= 0;i--) out.print(g[i]);
            out.println();

            for (int i = n-1;i >= 0;i--) out.print(g[i]);
        }

     //   out.println(res);

        out.println();
		out.close();
	}
}


import java.util.Scanner;
import java.util.ArrayList;

public class TwoButtons {
  public static void main(String[]args) {
    Scanner input = new Scanner(System.in);
    int cur = input.nextInt();
    int end = input.nextInt();

    ArrayList<Integer> q = new ArrayList<Integer>();
    q.add(cur);
    int index = 0;
    while (cur != end) {
      cur = q.get(index);
      q.add(cur*2);
      q.add(cur-1);
      index++;
    }
    System.out.println((int)(Math.log(index)/Math.log(2)));
  }
}


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

/**
 *
 * @author adrian
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
       Scanner l = new Scanner(System.in);

       int r = l.nextInt();
       int g = l.nextInt();
       int b = l.nextInt();

       P[] arr = new P[3];

       arr[0] = new P(0, r);
       arr[1] = new P(1, g);
       arr[2] = new P(2, b);

       Arrays.sort(arr);

        System.out.println(30 + (arr[0].cant / 2) + arr[0].color + 1);
    }

}

class P implements Comparable<P>{
    int color,cant;

    public P(int color, int cant) {
        this.color = color;
        this.cant = cant;
    }

    public int compareTo(P p) {
        if(cant - p.cant > 1 || (Math.abs(cant - p.cant) <= 1 && color > p.color))
            return -1;
        else
            return 1;
    }


import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int btcAmount = sc.nextInt();
        int[] btcCosts = new int[100];
        int even = 0, odd = 0;
        for (int i = 0; i < n; i++) {
            int left = sc.nextInt();
            if (left % 2 == 0) even++;
            else odd++;
            if (even == odd && i != n - 1) {
                int right = sc.nextInt();
                i++;
                int btcCost = Math.abs(left - right);
                btcCosts[btcCost] += 1;
                even = 0;
                odd = 0;
            }
        }
        int sawedAmount = 0;
        int currentBTCCost = 0;
        for (int i = 0; i < 100; i++) {
            for (int j = 0; j < btcCosts[i]; j++) {
                currentBTCCost += i;
                if (currentBTCCost <= btcAmount) {
                    sawedAmount++;
                } else break;
            }
        }
        System.out.println(sawedAmount);
    }


import java.util.HashSet;
import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        if (n >= 13) {
           System.out.println(solve(12) + (n-12)* 49);
        } else {
           System.out.println(solve(n));
        }
    }

    private static long solve(int n) {
        HashSet<Integer> set = new HashSet<>();
        set.add(1);
        set.add(5);
        set.add(10);
       set.add(50);
        for (int i = 2; i <= n ; i++) {
            HashSet<Integer> set2 = new HashSet<>();
            set.forEach((x) -> {
                set2.add(x+1);
                set2.add(x+5);
               set2.add(x+10);
                set2.add(x+50);
            });
            set = set2;
        }
        return set.size();
    }

}


// import org.junit.Test;
import java.util.Scanner;

// import static org.junit.Assert.assertEquals;

public class CF_1223G {
    static Scanner scanner = new Scanner(System.in);
    public static void main(String args[])
    {
         //test();
        long caseNum = 1;
        while (caseNum-- > 0) {
            task();
        }
    }

    private static int maxN = 500000;
    private static int[] a = new int[maxN + 10];
    private static int[] sum = new int[maxN + 10];
    private static int[] input = new int[maxN + 10];

    public static void task() {
        int n = scanner.nextInt();
        // System.out.println(n);
        for (int i = 0; i < n; i++) {
            input[i] = scanner.nextInt();
        }
        // System.out.println("input end");
        solve(n, input);
    }

    public static void clear(int n) {
        for (int i = 1; i <= maxN + 1; i++) {
            a[i] = 0;
            sum[i] = 0;
        }
    }

    private static int maxA;

    public static long solve(int n, int[] input) {
        long maxArea = 0;
        maxA = 0;
        clear(n);
        for (int i = 0; i < n; i++) {
            a[input[i]]++;
            if (input[i] > maxA) {
                maxA = input[i];
            }
        }
        for (int i = 2; i <= maxN + 1; i++) {
            sum[i] = a[i] + sum[i - 1];
        }

        for (int y = 2; y <= maxA; y++) {
            long total = 0;
            for (int step = y; step <= maxA; step += y) {
                total += q(step, min(step + y - 1, maxA) + 1) * (long) (step / y);
            }
            int x = 1;
            int left = 2;
            int right = maxA;
            while (left <= right) {
                int mid = (left + right) / 2;
                if (check(y, mid, total)) {
                    if (mid > x) {
                        x = mid;
                    }
                    left = mid + 1;
                } else {
                    right = mid - 1;
                }
            }
            if (x > 1) {
                if ((long) x * (long) y > maxArea) {
                    maxArea = (long) x * (long) y;
                    /*
                    System.out.println(y + " " + x);
                    System.out.println(maxArea);
                     */
                }
            }
        }
        System.out.println(maxArea);

        return maxArea;
    }

    public static boolean check(int y, int x, long total) {
        if (y == 4) {
           // System.out.println("fcuk");
        }
        boolean ret = check1(y, x, total, x + x, false) || check2(y, x, total);
        //System.out.println(y + " " + x + " " + total + " " + ret);
        return ret;
    }

    public static boolean check1(int y, int x, long total, int need, boolean ignore) {
        // together
        long up = (need - 1) / y + 1;
        long down = need / y;
        int hits = 0;
        if (!ignore) {
            if (need < y) {
                if (q(need, y) > 0) {
                    hits = 1;
                }
            }
        }
        if (hits == 1) {
            return total >= x;
        }
        int toSub = 0;
        int remain = need % y;
        for (int step = y; step + remain <= maxA; step += y) {
            if (q(step + remain, min(step + y,maxA + 1)) > 0) {
                toSub = 1;
                break;
            }
        }
        if (toSub == 1) {
            return total >= x + down;
        } else {
            if (q(need, maxA + 1) > 0) {
                return total >= x + up;
            } else {
                return false;
            }
        }
    }

    private static int min(int x, int y) {
        if (x < y) {
            return x;
        }
        return y;
    }

    public static boolean check2(int y, int x, long total) {
        // separate
        int need = x;
        int up = (need - 1) / y + 1;
        int down = need / y;
        int hits = 0;
        if (need < y) {
            if (q(need, y) > 0) {
                hits = q(need, y);
            }
            if (hits >= 2) {
                hits = 2;
            }
        }
        if (hits == 2) {
            return total >= x;
        }
        // ?
        if (hits == 1) {
            return check1(y, x, total, x, true);
        }
        if (q(need, maxA + 1) < 2){
            return false;
        }
        if (hits == 0) {
            int toSub = 0;
            int remain = need % y;
            for (int step = y; step + remain <= maxA; step += y) {
                int h = q(step + remain, min(step + y,maxA + 1));
                toSub += h;
                if (toSub >= 2) {
                    toSub = 2;
                    break;
                }
            }
            if (toSub == 2) {
                return total >= x + down + down;
            }
            if (toSub == 1) {
                return total >= x + down + up;
            }
            if (toSub == 0) {
                return total >= x + up + up;
            }
        }
        // never
        return false;
    }

    public static int q(int begin, int end) {
        if (begin > end) {
            return 0;
        }
        return sum[end - 1] - sum[begin - 1];
    }

    public static void test() {
        int n = 90792;
        int[] input = new int[maxN + 10];
        input[0] = 181565;
        input[1] = 181564;
        for (int i = 2; i < n; i++) {
            input[i] = 60522;
        }
        System.out.println(solve(n, input));
    }

    /*
    @Test
    public void test() {
        int n = 1;
        int[] input = {9};
        assertEquals(4, solve(n, input));
    }

    @Test
    public void test2() {
        String s = "9\n" +
                "9 10 9 18 9 9 9 28 9";
        s = "9\n" +
                "9 10 9 19 9 9 9 28 9";
        Scanner scanner = new Scanner(s);
        int n = scanner.nextInt();
        int [] input = new int[500000 +100];
        for (int i = 0; i < n; i++) {
            input[i] = scanner.nextInt();
        }
        assertEquals(90, solve(n, input));
    }
     */
}


//package round60;

import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class A {
	Scanner in;
	PrintWriter out;
	String INPUT = "";

	void solve()
	{
		int a = ni();
		int b = ni();
		int c = ni();
		int d = ni();
		int e = ni();
		int f = ni();
		if(d * b > a * c || (long)f*b*d > (long)e*a*c){
			out.println("Ron");
		}else{
			out.println("Hermione");
		}
	}

	void run() throws Exception
	{
		in = INPUT.isEmpty() ? new Scanner(System.in) : new Scanner(INPUT);
		out = new PrintWriter(System.out);

		solve();
		out.flush();
	}

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

	int ni() { return Integer.parseInt(in.next()); }
	void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class A {
    public static void main(String args[]) throws NumberFormatException, IOException{
    	BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    	String s[]=br.readLine().split(" ");
    	int n=Integer.parseInt(s[0]);
    	int b=Integer.parseInt(s[1]);
    	int p=Integer.parseInt(s[2]);
    	int bcount=0;
    	int tcount=n*p;
    	for(;;){
    		int k=1;
        	for(;k<n;){
        		k=k*2;
        	}
        	//System.out.println(k+" "+n+"yes");
    		int r=n-k;
    		bcount=bcount+(k/2)*(2*b+1);
    		k=k/2;
    		n=k+r;
    		//System.out.println(k+" "+n+"yes");
    		if(n==1){
    			break;
    		}
    	}
    	System.out.println(bcount+" "+tcount);
    }
}


import java.util.Scanner;

public class 999B {
	public static void main (String[] args) {
		Scanner scan = new Scanner(System.in);

		int n = scan.nextInt();
		scan.nextLine();

		String k = scan.nextLine();
		String temp = "";

		for(int i=2; i<=n; i++) {
		    if(n%i == 0) {
		        for(int j=i-1; j>=0; j--) {
		            temp += k.charAt(j);
		        }
		        for(int j=i; j<n; j++) {
		            temp += k.charAt(j);
		        }
		        k = temp;
		        temp = "";
		    }
		}

		System.out.println(k);

	}
}


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

public class Main {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n, m, a;
        n = input.nextInt();
        m = input.nextInt();
        a = input.nextInt();

        int[][] array = new int[n][m];
        int result = 0;

        for (int i = 0; i < n; i += a) {
            int x = i - (n - i);
            if (x > 0) {
                i = x;
            }
            for (int j = 0; j < m; j += a) {
                int y = j - (m - j);
                if (y > 0) {
                    j = y;
                }
                if (array[i][j] == 1) {
                    break;
                }
                array[i][j] = 1;
                result++;
            }
        }
        System.out.print(result);
    }
}


// package mainpackage;

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

public class Main {
	public static void main(String... args) throws Exception {
		new Main().solve();
	}

	private BufferedReader reader;
	private int n;

	private long[] pow7;
	private long[][] c;
	private long ans = 0;

	private static final long MOD = (long)1e9 + 7;

	private void solve() throws Exception {

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

		n = Integer.parseInt(reader.readLine());

		reader.close();

		pow7 = new long[n + 1];
		c = new long[n + 1][n / 2 + 1];

		c[0][0] = 0;
		pow7[0] = 1;
		for (int i = 1; i <= n; ++i) {
			c[i][0] = 1;

			if (i <= n / 2) {
				c[0][i] = 1;
			}
		}

		for (int i = 1; i <= n; ++i) {
			pow7[i] = (pow7[i - 1] * 7) % MOD;

			for (int j = 1; j <= i / 2; ++j) {
				int j1 = (j - 1 <= (i - 1) / 2) ? j - 1 : i - j;
				int j2 = (j <= (i - 1) / 2) ? j : i - j - 1;

				c[i][j] = (c[i - 1][j1] + c[i - 1][j2]) % MOD;
			}
		}

		long pow20 = 1;
		for (int i = 1; i <= n; ++i) {

			pow20 *= 20;
			pow20 %= MOD;
			int indx = (i <= n / 2) ? i : n - i;
			ans += (c[n][indx] * pow7[n - i] * pow20) % MOD;
//			System.out.printf("pow20[%d]=%d%npow7[%d]=%d%nC[%d][%d]=%d%n%n", i, pow20[i], n - i, pow7[n - i],
//					n, i, C[n][(i <= n / 2) ? i : (n - i)]);
			ans %= MOD;
		}

		System.out.println(ans);
	}
}


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

public class BayanA {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        System.out.println("+------------------------+");
        for(int i=0; i<4; i++){
            if(i==2){
                if(n>2) System.out.print("|O"); else System.out.print("|.");
                System.out.println(".......................|");
                continue;
            }
            System.out.print("|");
            for(int j=0; j<11; j++){
                int odd = 0;
                if(j>0 || i>2) odd=1;
                int ii=i;
                if(i>2) ii--;
                if(n>3*j+odd+ii) System.out.print("O."); else System.out.print("#.");
            }
            if(i==0) System.out.print("D"); else System.out.print(".");
            System.out.print("|");
            if(i<1 || i>2) System.out.print(")");
            System.out.println();
        }
        System.out.println("+------------------------+");
    }
}


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

     //--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<-------------------

public class a{
     static long[] count,count1,count2;
     static Node[] nodes;
     static long[] arr;
     static int[] dp;
     static char[] ch,ch1;
     static long[] darr,farr;
     static char[][] mat,mat1;
     static long x,h;
     static long maxl;
     static double dec;
     static long mx = (long)1e9+7;
     static String s;
     static long minl;
     static int start_row;
     static int start_col;
     static int end_row;
     static int end_col;
     static long mod = (long)1e7+7;
     // static int minl = -1;
     // static long n;
     static int n,n1,n2;
     static long a;
     static long b;
     static long c;
     static long d;
     static long y,z;
     static int m;
     static int k;
     static String[] str,str1;
     static Set<Long> set,set1,set2;
     static SortedSet<Long> ss;
     static List<Integer> list,list1,list2,list3;
     static LinkedList<Node> ll;
     static Map<Integer,List<Integer>> map,map1;
     static StringBuilder sb,sb1,sb2;
     static int index;
     static int[] dx = {0,-1,0,1,-1,1,-1,1};
     static int[] dy = {-1,0,1,0,-1,-1,1,1};

     // public static void solve(){

     //    FastScanner sc = new FastScanner();
     //    // int t = sc.nextInt();
     //    int t = 1;
     //    for(int tt = 0 ; tt < t ; tt++){
     //        int n = sc.nextInt();
     //        int m = sc.nextInt();
     //        int prev = 0;

     //        for(int i = 0 ; i < n ; i++){
     //           int s = sc.nextInt();
     //           int e = sc.nextInt();
     //           if(s > prev){
     //                System.out.println("NO");
     //                return;
     //           }
     //            prev = Math.max(prev,e);
     //        }
     //        if(prev == m)
     //            System.out.println("YES");
     //        else
     //            System.out.println("NO");

     //        // System.out.println(sb);
     //    }

     // }

     //--------------->>>>IF YOU ARE HERE FOR QUICKSORT HACK THEN SORRY NO HACK FOR YOU<<<-------------------

     public static void solve(){

        if(y == b){
            System.out.println((2*Math.abs(x-a) + 4)+2);
            return;
        }

        if(x == a){
            System.out.println(4 + 2*Math.abs(y-b)+2);
            return;
        }

        if(y < b){
            y -= 1;
            x += 1;
        }
        else if(y > b){
            y += 1;
            x += 1;
        }

        System.out.println(2*(Math.abs(x-a) + Math.abs(y-b)));

     }

     public static void main(String[] args) {

            FastScanner sc = new FastScanner();
            // Scanner sc = new Scanner(System.in);
            // int t = sc.nextInt();
            int t = 1;
            // int l = 1;
            while(t > 0){

                // n = sc.nextInt();
                // n = sc.nextLong();
                a = sc.nextLong();
                b = sc.nextLong();
                // c = sc.nextLong();
                // d = sc.nextLong();

                x = sc.nextLong();
                y = sc.nextLong();

                // n = sc.nextLong();
                // n = sc.nextInt();
                // n1 = sc.nextInt();

                // m = sc.nextInt();

                // k = sc.nextInt();
                // s = sc.next();

                // ch = sc.next().toCharArray();
                // ch1 = sc.next().toCharArray();

                // arr = new int[n];
                // for(int i = 0 ; i < n ; i++){
                //     arr[i] = sc.nextInt();
                // }
                // // m = n;
                // darr = new long[n];
                // for(int i = 0; i < n ; i++){
                //     darr[i] = sc.nextLong();
                // }

                // farr = new int[n];
                // for(int i = 0; i < n ; i++){
                //     farr[i] = sc.nextInt();
                // }

                // mat = new char[n][m];
                // for(int i = 0 ; i < n ; i++){
                //     String s = sc.next();
                //     for(int j = 0 ; j < m ; j++){
                //         mat[i][j] = s.charAt(j);
                //     }
                // }

                // str = new String[n];
                // for(int i = 0 ; i < n ; i++)
                //     str[i] = sc.next();

                // nodes = new Node[n];
                // for(int i = 0 ; i < n ;i++)
                //     nodes[i] = new Node(sc.nextInt(),sc.nextInt());

                // System.out.println(solve()?"YES":"NO");
                 solve();
                 // System.out.println(solve());
                 t -= 1;
            }

     }

    public static int log(long n){

         if(n == 0 || n == 1)
             return 0;

         if(n == 2)
             return 1;

         double num = Math.log(n);
         double den = Math.log(2);

         if(den == 0)
             return 0;

         return (int)(num/den);
     }

    public static boolean isPrime(long n) {
    // Corner cases
        if (n <= 1)
            return false;

        if (n <= 3)
            return true;

        // This is checked so that we can skip
        // middle five numbers in below loop
        if (n%2 == 0 || n%3 == 0)
            return false;

        for (int i=5; i*i<=n; i=i+6)
            if (n%i == 0 || n%(i+2) == 0)
               return false;

        return true;
    }

     public static long gcd(long a,long b){

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

     }

     // public static void swap(int i,int j){
     //     long temp = arr[j];
     //     arr[j] = arr[i];
     //     arr[i] = temp;
     // }

    static final Random random=new Random();

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

    static class Node{
        Integer first;
        Integer second;
        Node(Integer f,Integer s){
            this.first = f;
            this.second = s;
        }
    }

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

}


//package cf213;

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

public class B {
	FastScanner in;
	PrintWriter out;

	public void solve() throws IOException {
		final int MAX_C = (int) 1e4;
		int n = in.nextInt();
		int d = in.nextInt();
		int[] c = new int[n];
		for (int i = 0; i < n; i++) {
			c[i] = in.nextInt();
		}
		Arrays.sort(c);
		int sum = 0;
		for (int i = 0; i < n; i++) {
			if (sum + d >= c[i]) {
				sum += c[i];
			} else {
				break;
			}
		}

		boolean[][] dp = new boolean[n + 1][n * MAX_C];
		dp[0][0] = true;
		for (int i = 1; i <= n; i++) {
			for (int j = 0; j < n *MAX_C; j++) {
				dp[i][j] |= dp[i - 1][j];
			}
			for (int j = 0; j < (n - 1) * MAX_C; j++) {
				dp[i][j + c[i - 1]] |= dp[i - 1][j];
			}
		}
		int curSum = 0;
		int  ans = 0;
		while (curSum < sum) {
			for (int i = curSum + d; i > curSum; i--) {
				if (dp[n][i]) {
					curSum = i;
					break;
				}
			}
			ans++;
		}
		System.out.println(sum + " " + ans);

	}

	public void run() {
		try {
			in = new FastScanner(System.in);
			out = new PrintWriter(new File("object.out"));

			solve();

			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	class FastScanner {
		BufferedReader br;
		StringTokenizer st;

		FastScanner(File f) {
			try {
				br = new BufferedReader(new FileReader(f));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}

		FastScanner(InputStream f) {
			br = new BufferedReader(new InputStreamReader(f));
		}

		String next() {
			while (st == null || !st.hasMoreTokens()) {
				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());
		}
	}

	public static void main(String[] arg) {
		new B().run();
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;

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

    static class CPermuteDigits {
        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            char a[] = in.scanString().toCharArray();
            char b[] = in.scanString().toCharArray();
            int count[] = new int[10];
            for (char k : a) count[k - '0']++;

            if (a.length < b.length) {
                for (int i = 9; i >= 0; i--) {
                    while (count[i] > 0) {
                        out.print(i);
                        count[i]--;
                    }
                }
                out.println();
            } else {
                if (a.length > b.length) {
                    for (int i = 0; i < a.length - b.length; i++) out.print(0);
                    count[0] -= (a.length - b.length);
                    if (count[0] < 0) throw new RuntimeException("ERE");
                    out.println(findIT(count, b));
                } else {
                    out.println(findIT(count, b));
                }
            }
        }

        String findIT(int[] count, char[] b) {
            boolean flag = false;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < b.length; i++) {
                int index = -1;
                for (int j = 9; j >= 0; j--) {
                    if (count[j] > 0) {
                        if (flag) {
                            index = j;
                            break;
                        } else {
                            if (j <= b[i] - '0') {
                                index = j;
                                break;
                            }
                        }
                    }
                }

                if (index == -1) throw new RuntimeException();
                if (index < (b[i] - '0')) flag = true;
                count[index]--;
                sb.append(index);
            }
            return sb.toString();
        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int INDEX;
        private BufferedInputStream in;
        private int TOTAL;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (INDEX >= TOTAL) {
                INDEX = 0;
                try {
                    TOTAL = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (TOTAL <= 0) return -1;
            }
            return buf[INDEX++];
        }

        public String scanString() {
            int c = scan();
            while (isWhiteSpace(c)) c = scan();
            StringBuilder RESULT = new StringBuilder();
            do {
                RESULT.appendCodePoint(c);
                c = scan();
            } while (!isWhiteSpace(c));
            return RESULT.toString();
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

    }
}


//package CodeForces;

/*In the name of Allah the Most Merciful.
 * Author
 * Md. Toufiqul Islam
 * Dept. Of CSE
 * Ahsanullah University Of Science And Technology
*/
import java.io.*;
import java.util.*;
import java.math.*;
public class ER36B {

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

        int n = sc.nextInt();
        int pos = sc.nextInt();
        int pos1 = pos;
        int l = sc.nextInt();
        int r = sc.nextInt();

        int a = 1;
        int b = n;
        int totalSec =0;
        int totalSec1 =0;
        if(a==l && b==r){
            out.println("0");
        }

        else{

           if(a!=l && b!=r){

               while (pos1!=l){
                   pos1--;
                   totalSec++;
               }

               totalSec++;

               while (pos1!=r){
                   pos1++;
                   totalSec++;
               }
               totalSec++;

               pos1 = pos;

            //   System.out.println("Pos1 "+pos1);

               while (pos1!=r){
                   pos1++;
                   totalSec1++;
               //    System.out.println(totalSec1);

               }

               totalSec1++;

               while (pos1!=l){
                   pos1--;
                   totalSec1++;

               }
            //   System.out.println();
               totalSec1++;

             //  System.out.println(totalSec+" "+totalSec1);
               out.println(Math.min(totalSec,totalSec1));
           }
           else if(a!=l && b==r){
               while (pos1!=l){
                   pos1--;
                   totalSec++;
               }

               totalSec++;

               System.out.println(totalSec);
           }

           else if(a==l && b!=r){
            //   System.out.println(pos1+" "+r);

               while (pos1!=r){
                   pos1++;
                   totalSec1++;
                   //    System.out.println(totalSec1);

               }

               totalSec1++;

               //   System.out.println();

               System.out.println(totalSec1);

           }
           else{
               System.out.println("0");
           }
        }
        out.close();
    }

    public static class InputReader {
        private boolean finished = false;

        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 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 < '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 {
                if (Character.isValidCodePoint(c)) {
                    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 readLine() {
            String s = readLine0();
            while (s.trim().length() == 0) {
                s = readLine0();
            }
            return s;
        }

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

        public BigInteger readBigInteger() {
            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);
        }
        public int[] nextIntArray(int n){
            int[] array=new int[n];
            for(int i=0;i<n;++i)array[i]=nextInt();
            return array;
        }
        public int[] nextSortedIntArray(int n){
            int array[]=nextIntArray(n);
            Arrays.sort(array);
            return array;
        }
        public int[] nextSumIntArray(int n){
            int[] array=new int[n];
            array[0]=nextInt();
            for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt();
            return array;
        }
        public long[] nextLongArray(int n){
            long[] array=new long[n];
            for(int i=0;i<n;++i)array[i]=nextLong();
            return array;
        }
        public long[] nextSumLongArray(int n){
            long[] array=new long[n];
            array[0]=nextInt();
            for(int i=1;i<n;++i)array[i]=array[i-1]+nextInt();
            return array;
        }
        public long[] nextSortedLongArray(int n){
            long array[]=nextLongArray(n);
            Arrays.sort(array);
            return array;
        }
    }
    public 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 print(char[] array) {
            writer.print(array);
        }

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

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

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

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

        public void println(int[] array) {
            print(array);
            writer.println();
        }

        public void println(double[] array) {
            print(array);
            writer.println();
        }

        public void println(long[] array) {
            print(array);
            writer.println();
        }

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

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

        public void print(char i) {
            writer.print(i);
        }

        public void println(char i) {
            writer.println(i);
        }

        public void println(char[] array) {
            writer.println(array);
        }

        public void printf(String format, Object... objects) {
            writer.printf(format, objects);
        }

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

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

        public void print(long i) {
            writer.print(i);
        }

        public void println(long i) {
            writer.println(i);
        }

        public void print(int i) {
            writer.print(i);
        }

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

        public void separateLines(int[] array) {
            for (int i : array) {
                println(i);
            }
        }
    }

}


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

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

    static class TaskD {
        static int N;
        static int K;
        static int MOD;
        static int memo;
        static int[] pow10;

        static int dp(int reached, int modSoFar, int idx) {
            if (idx == N) return reached;
            if (memo[reached][modSoFar][idx] != -1)
                return memo[reached][modSoFar][idx];

            int res = 0;
            for (int put = idx == N - 1 ? 1 : 0; put <= 9; put++) {
                int nxtMod = (pow10[idx] * put + modSoFar) % K;
                int nxtReached = reached == 1 || (put != 0 && nxtMod == 0) ? 1 : 0;
                res += dp(nxtReached, nxtMod, idx + 1);
                if (res >= MOD) res -= MOD;
            }
            return memo[reached][modSoFar][idx] = res;
        }

        public void solve(int testNumber, Scanner sc, PrintWriter out) {
            N = sc.nextInt();
            K = sc.nextInt();
            MOD = sc.nextInt();
            pow10 = new int[N];
            pow10[0] = 1;
            for (int i = 1; i < N; i++)
                pow10[i] = (int) (10L * pow10[i - 1] % K);
            memo = new int[2][K + 1][N + 1];
            for (int[][] mm : memo) for (int[] m : mm) Arrays.fill(m, -1);
            out.println(dp(0, 0, 0));
        }

    }

    static 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() {
            while (st == null || !st.hasMoreTokens())
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (Exception e) {
                }
            return st.nextToken();
        }

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

    }
}


package Div2_630;

import java.util.Scanner;

public class E {
    static long mod = 998244353;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long n = in.nextInt();
        long m = in.nextInt();
        long p = in.nextInt();
        long q = in.nextInt();
        long x = q - p + 1;
        if (p % 2 == 0) x = x - x / 2;
        else x /= 2;
        long y = q - p + 1 - x;
        if ((n & m) % 2 == 0) {
            long ans = fn(x, y, n * m, 0);
            System.out.println(ans);
        } else {
            long ans = bin(x + y, n * m);
            System.out.println(ans);
        }
    }

    private static long fn(long x, long y, long st, int type) {
        if (st == 1) {
            if (type == 0) return x;
            return y;
        }
        if (st % 2 == 1) {
            return (x * fn(x, y, st - 1, type) + y * fn(x, y, st - 1, (type + 1) % 2)) % mod;
        } else {
            long nch = fn(x, y, st / 2, 1);
            long ch = fn(x, y, st / 2, 0);
            if (type == 0) {
                return ((ch * ch) % mod + (nch * nch) % mod) % mod;
            } else {
                return (2 * ch * nch) % mod;
            }
        }
    }

    private static long bin(long x, long st) {
        if (st == 0) return 1;
        if (st == 1) return x;
        long m = st / 2;
        long ans = bin(x, m);
        if (st % 2 == 0) {
            return (ans * ans) % mod;
        } else {
            return (((ans * ans) % mod) * x) % mod;
        }
    }
}


import java.util.Scanner;

public class E {
    static long mod = 998244353;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        long n = in.nextInt();
        long m = in.nextInt();
        long p = in.nextInt();
        long q = in.nextInt();
        long x = q - p + 1;
        if (p % 2 == 0) x = x - x / 2;
        else x /= 2;
        long y = q - p + 1 - x;
        if ((n & m) % 2 == 0) {
            long ans = fn(x, y, n * m, 0);
            System.out.println(ans);
        } else {
            long ans = bin(x + y, n * m);
            System.out.println(ans);
        }
    }

    private static long fn(long x, long y, long st, int type) {
        if (st == 1) {
            if (type == 0) return x;
            return y;
        }
        if (st % 2 == 1) {
            return (x * fn(x, y, st - 1, type) + y * fn(x, y, st - 1, (type + 1) % 2)) % mod;
        } else {
            long nch = fn(x, y, st / 2, 1);
            long ch = fn(x, y, st / 2, 0);
            if (type == 0) {
                return ((ch * ch) % mod + (nch * nch) % mod) % mod;
            } else {
                return (2 * ch * nch) % mod;
            }
        }
    }

    private static long bin(long x, long st) {
        if (st == 0) return 1;
        if (st == 1) return x;
        long m = st / 2;
        long ans = bin(x, m);
        if (st % 2 == 0) {
            return (ans * ans) % mod;
        } else {
            return (((ans * ans) % mod) * x) % mod;
        }
    }
}


import java.util.Scanner;

public class main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int house = 1234567;
		int car = 123456;
		int computer = 1234;
		boolean[] dp = new boolean[n + 1];
		dp[0] = true;
		for (int i = 1; i < n+1; i++) {
			if (i >= 1234567) dp[i] = dp[i - 1234567];
			if (dp[i] == false ) {
				if (i >= 123456) dp[i] = dp[i - 123456];
			}
			if (dp[i] == false ) {
				if (i >= 1234) dp[i] = dp[i - 1234];
			}
		}
		System.out.println(dp[n] ? "YES": "NO");
	}
}


package sample;

import java.util.ArrayList;
import java.util.Scanner;

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

        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();
        int d = sc.nextInt();

        int big = 0;
        ArrayList<Integer> list = new ArrayList<>();

        if (a > big) {
            big = a;
            list.clear();
            list.add(1);
        }
        if (b > big) {
            big = b;
            list.clear();
            list.add(2);
        }
        if (c > big) {
            big = c;
            list.clear();
            list.add(3);
        }
        if (d > big) {
            big = d;
            list.clear();
            list.add(4);
        }

        if (list.get(0) == 1) {
            a = 0;
        }
        if (list.get(0) == 2) {
            b = 0;
        }
        if (list.get(0) == 3) {
            c = 0;
        }
        if (list.get(0) == 4) {
            d = 0;
        }

        if ((big >= (a + b + c)) || (big >= (a + c + d)) || (big >= (b + c + d)) || (big >= (a + b + d))) {
            System.out.println("NO");
        }
        else {
            System.out.println("YES");
        }
    }
}


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

@SuppressWarnings("unused")
public class D {
	public static void main(String[] args) throws Exception {
		Scanner in = new Scanner(System.in);
		int b = in.nextInt();
		int d = in.nextInt();
		String a = in.next();
		String c = in.next();

		for (int i = 0; i < c.length(); i++) {
			if (a.contains("" + c.charAt(i)) == false) {
				System.out.println(0);
				return;
			}
		}

		long lo = 0, hi = 2000000000;
		while (hi - lo > 1) {
			long mid = (lo + hi) / 2;
			if (needed(a, c, mid * d) <= a.length() * b) {
				lo = mid;
			} else {
				hi = mid;
			}
		}
		System.out.println(lo);
	}

	static long needed(String a, String c, long count) {
		long[][] next = new long[50][a.length()];
		for (int i = 0; i < next[0].length; i++) {
			long free = i;
			for (int j = 0; j < c.length(); j++) {
				while (c.charAt(j) != a.charAt((int) (free % a.length()))) {
					free++;
				}
				free++;
			}
			next[0][i] = free;
		}
		for (int i = 1; i < next.length; i++) {
			for (int j = 0; j < next[i].length; j++) {
				next[i][j] += j;
				long idx1 = next[i - 1][j];
				next[i][j] += idx1 - j;
				idx1 %= a.length();
				long idx2 = next[i - 1][(int) idx1];
				next[i][j] += idx2 - idx1;
			}
		}
		long ans = 0;
		long free = 0;
		while (count > 0) {
			long bit = count & -count;
			count -= bit;
			int id = Long.numberOfTrailingZeros(bit);
			ans += next[id][(int) free] - free;
			free = next[id][(int) free];
			free %= a.length();
		}
		return ans;
	}
}


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

public class Main {

    public static void main(String[] args) throws Exception {
        Reader.init(System.in);
        int k=Reader.nextInt();
        int l=Reader.nextInt();
        int x=1;
        if(l%k==0)
        {
        for (int i = 0;; i++) {
            if(Math.pow(k, x)==l)
            {
                System.out.println("YES\n"+ (x-1));
                return;
            }
            x++;
        }
        }
        else
        System.out.println("NO");

    }
    public static boolean S(int [][]a,int n , int s , int d)
    {
        for (int i = 0; i < n; i++) {
            s+=a[i][0];
            d+=a[i][1];
        }
        return s%2==0&&d%2==0;
    }

}
class Reader {

    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /**
     * call this method to initialize reader for InputStream
     */
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    /**
     * get next word
     */
    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine());
        }
        return tokenizer.nextToken();
    }

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

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

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

    static public void nextIntArrays(int[]... arrays) throws IOException {
        for (int i = 1; i < arrays.length; ++i) {
            if (arrays[i].length != arrays[0].length) {
                throw new InputMismatchException("Lengths are different");
            }
        }
        for (int i = 0; i < arrays[0].length; ++i) {
            for (int[] array : arrays) {
                array[i] = nextInt();
            }
        }
    }

    static public void nextLineArrays(String[]... arrays) throws IOException {
        for (int i = 1; i < arrays.length; ++i) {
            if (arrays[i].length != arrays[0].length) {
                throw new InputMismatchException("Lengths are different");
            }
        }
        for (int i = 0; i < arrays[0].length; ++i) {
            for (String[] array : arrays) {
                array[i] = reader.readLine();
            }
        }
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Vector;

public class Main {

    public static void main(String[] args) {
        FastReader read = new FastReader();
        int p = read.nextInt() ;
        int y = read.nextInt() ;

            int ans = calc(p,y);
            System.out.println(ans);

    }
     static int calc(int n,int y)
    {
      boolean prime[] = new boolean[y+1];

        for(int p = 3; p*p <=n; p+=2)
        {
            if(prime[p] == false)
            {

                for(int i = p*2; i <= y; i += p)
                    prime[i] = true;
            }
        }
         if(y%2==0)
             y--;
        for(int i = y; i > n; i-=2)
        {
            if(prime[i] == false)
                return i ;
        }
        return -1 ;
    }

    }

class Pair implements Comparable<Pair> {

    int x, y;

    public Pair(int x, int y) {
        this.x = x;
        this.y = y;

    }

    @Override
    public int compareTo(Pair p) {
        if (this.x != p.x) {
            return this.x - p.x;
        } else {
            return p.y - this.y;
        }

    }

}

class FastReader {

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


import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Rib {
	public static void main(String args[])
	{
		Scanner in = new Scanner(System.in);
		int len = in.nextInt();
		int count = 0;
		int kp =0;
		int storep = 0;
		ArrayList<Integer> store = new ArrayList<Integer>();
		int[] k = new int[3];
		for(int i = 0; i<3; i++)
		{
			k[i]=in.nextInt();
		}
		Arrays.sort(k);

		if(len%k[0]==0)
			System.out.println(len/k[0]);
		else
		{
			int time = 0;
			while(len!=0)
			{
				if(len<0)
				{
					if(kp<2)
					{
						kp++;
						count-=2;
						len = store.remove(--storep);
						len = store.remove(--storep);
					}
					else
					if(time== 0)
					{
						kp=1;
						count=0;
						len = store.get(0);
						time++;
					}
					else
					{
						kp = 2;
						count = 0;
						len = store.get(0);
					}
				}
				else
				{
					store.add(storep++,len);
					len-=k[kp];
					count++;
				}

			}
//			for(Integer i: store)
//			{
//				System.out.println(i);
//			}
			System.out.println(count);
		}
	}
}


import java.math.BigInteger;
import java.util.Scanner;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author root
 */
public class TheBigRace {
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);
        long t = in.nextLong();
        long w = in.nextLong();
        long b = in.nextLong();

//        if(b == w){
//            System.out.println(1);
//            return;
//        }

        BigInteger lcm = lcm(w,b);
//        System.out.println("lcm= "+lcm);
        long min = Math.min(w,b);
        BigInteger bigT = new BigInteger(t+"");
        BigInteger bigM = new BigInteger(min+"");
        long ans = (((bigT.divide(lcm)).multiply(bigM)).add(min((bigT.mod(lcm)), bigM.subtract(new BigInteger("1"))))).longValue();
//        long ans = (t/lcm)*min+Math.min(t%lcm, min-1);
        long gcd = gcd(t, ans);
//        System.out.println("gcd = "+gcd);
        System.out.println((ans/gcd)+"/"+(t/gcd));
    }

    public static BigInteger min(BigInteger a, BigInteger b){
        if(a.compareTo(b)<0){
            return a;
        }
        return b;
    }

    public static BigInteger lcm(long a, long b){
        BigInteger res = new BigInteger("0");
        BigInteger bigA = new BigInteger(""+a);
        BigInteger bigB = new BigInteger(""+b);
        BigInteger gcd =  new BigInteger(""+gcd(a,b));
        res = (bigA.multiply(bigB)).divide(gcd);
        return res;
    }

    public static long gcd(long a, long b){
        if(a%b == 0){
            return b;
        }
        if(b%a == 0){
            return a;
        }

        return gcd(b, a%b);
    }
}


import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;

public class myFile
{
	public static void main(String args[])
	{
	   Scanner sc = new Scanner(System.in);
	   int i,n,k;
	   while(sc.hasNextInt())
	   {
		   n = sc.nextInt();
		   k = sc.nextInt();
		   sc.nextLine();
		   String str = sc.nextLine();
		   if(n==1 && k==1)
		   {
			   System.out.println("0");
			   continue;
		   }
		   String ans="";
		   if(str.charAt(0)!='1')
			   k--;
		   ans+='1';
		   for(i=1;k>0 && i<str.length();i++)
		   {
			   ans+='0';
			   if(str.charAt(i)!='0')k--;
		   }
		   ans = ans+str.substring(i);
		   System.out.println(ans);
	   }

	}

}


// Working program with FastReader
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.util.*;

public class Main {
	static class FastReader {
		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;
		}
	}

	public static void main(String[] args) throws IOException {
		FastReader in=new FastReader();
		BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
		int n = in.nextInt();
		int k = in.nextInt();
		if(k > 0){
			char[] s = in.nextLine().toCharArray();

			if(s[0] != '1') {
				s[0] = '1';
				k--;
			}
			if(k == 0)
				s[0]= '0';
			for(int i = 1; i<=k; i++){
				s[i]= '0';
			}

			log.write(new String(s));
		}else{
			log.write(in.nextLine());
		}

		log.flush();
		log.close();

	}
}


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

public class problem2 {

    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        char let[] = br.readLine().toCharArray();
        String ans = "", st1 = "", st2 = "", tempans = "";
        int count = 0, temp = 0;
        for (int i = 0; i < n; i++) {
            try {
                st1 = "" + (let[i]);
                st2 = "" + (let[i + 1]);
                tempans = (st1 + st2);
                temp = 0;
                for (int j = 0; j < n; j++) {
                    st1 = "" + (let[j]);
                    st2 = "" + (let[j + 1]);
                    if (tempans.equals(st1 + st2)) {
                        temp++;
                    }
                    if (temp > count) {
                        count = temp;
                        ans = tempans;

                    }
                }
            } catch (Exception e) {

            }
        }

        for (int i = n - 1; i >= 0; i--) {
            try {
                st1 = "" + (let[i]);
                st2 = "" + (let[i - 1]);
                tempans = "" + (st1 + st2);
                temp = 0;
                for (int j = n - 1; j >= 0; j--) {
                    st1 = "" + (let[j]);
                    st2 = "" + (let[j - 1]);
                    for (int k = n - 1; k >= 0; k--) {
                        try {
                            st1 = "" + (let[k]);
                            st2 = "" + (let[k - 1]);
                            tempans = "" + (st1 + st2);
                            temp = 0;
                            for (int l = n - 1; l >= 0; l--) {
                                st1 = "" + (let[l]);
                                st2 = "" + (let[l - 1]);
                                if (tempans.equals(st1 + st2)) {
                                    temp++;
                                }
                                if (temp > count) {
                                    count = temp;
                                    ans = tempans;
                                }
                            }
                        } catch (Exception e) {

                        }
                    }
                    if (tempans.equals(st1 + st2)) {
                        temp++;
                    }
                    if (temp > count && tempans.equals(tempans.toUpperCase())) {
                        count = temp;
                        ans = tempans;
                    }
                }
            } catch (Exception e) {

            }
        }
        System.out.println(ans);
    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;

public class codeforces
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        String s=br.readLine();
        codeforces o=new codeforces();
        String[] t=s.split(" ");
        int n=Integer.parseInt(t[0]);
        int m=Integer.parseInt(t[1]);
        for(int i=n+2;;i+=2)
        {
            if(o.isPrime(i))
            {
                if(i==m)
                {
                    System.out.println("YES");
                    break;
                }
                else
                {
                    System.out.println("NO");
                    break;
                }
            }
        }
    }
    boolean isPrime(int num)
    {
        if(num%2==0&&num!=2)return false;
        if(num==2)return true;
        for(int i=3;i*i<=num;i+=2)
        {
            if(num%i==0)
            return false;
        }
        return true;
    }
}


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

public class TaskA {
  public static void main(String[] args) {
    TaskA tA = new TaskA();
    PrintWriter pw = new PrintWriter(System.out);
    tA.solve(new Scanner(System.in), pw);
    pw.close();
  }

  public void solve(Scanner input, PrintWriter output) {
    int n = input.nextInt();

    int current = input.nextInt();
    int previous = -1;
    for (int i = 1; i < n; i++) {
      previous = current;
      current = input.nextInt();
    }

    if (current == 0) {
      output.println("UP");
      return;
    }

    if (current == 15) {
      output.println("DOWN");
      return;
    }

    if (n == 1) {
      output.plrintln("-1");
      return;
    }

    if (current > previous) {
      output.println("UP");
    } else {
      output.println("DOWN");
    }
  }
}


package DIV2;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.Buffer;
import java.util.*;

/**
 * Created by dell on 12/11/2017.
 */
public class ques1 {
    static class FastReader
    {
        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;
        }
    }
    public static void main(String args[])throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        char c[][] = new char[10][10];
        for(int k=1;k<=9;k++)
        {   for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                c[k][(i-1)*3+j] = (char) br.read();
            }
            br.read();
        }
        if(k%3==0 && k!=9)
            br.readLine();
        }
        String s=br.readLine();
        int x=Integer.parseInt(s.substring(0,s.indexOf(" ")));
        int y=Integer.parseInt(s.substring(s.indexOf(" ")+1));
        int cx=(int)(Math.ceil(x*1.0/3));
        int cy=(int)(Math.ceil(y*1.0/3));
        int newx=((x-(cx-1)*3)-1)*3+1;
        int newy=(y-(cy-1)*3-1)*3+1;
        int f=0;
        /*for(int k=1;k<=9;k++)
        {   for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                System.out.print(c[k][(i-1)*3+j]);
            }
            System.out.print(" ");
        }
            System.out.println();
        }*/
     //   System.out.print(newx);
       // System.out.print(newy+"\n");
        for(int i=newx;i<=newx+2;i++)
        {
            for(int j=newy;j<=newy+2;j++)
            {
                if(c[i][j]=='.')
                {
                    c[i][j]='!';
                    f=1;
                }
            }
        }
        if(f==0)
        {
           // System.out.print(newx);
           // System.out.print(newy);

            for(int i=1;i<=9;i++)
            {
                for(int j=1;j<=9;j++)
                {
                    if(c[i][j]=='.')
                    {
                        c[i][j]='!';
                    }
                }
            }

        }
        for(int k=1;k<=9;k++)
        {   for (int i = 1; i <= 3; i++) {
            for (int j = 1; j <= 3; j++) {
                System.out.print(c[k][(i-1)*3+j]);
            }
            System.out.print(" ");
        }
        System.out.println();
        if(k%3==0 && k!=9)
            System.out.println();
        }
    }
}


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

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
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);
        DMagicGems solver = new DMagicGems();
        solver.solve(1, in, out);
        out.close();
    }

    static class DMagicGems {
        private int MOD = (int) 1e9 + 7;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            long n = in.nextLong();
            int m = in.nextInt();

            long[][] M = new long[m][m];
            M[0][0] = 1;
            M[0][m - 1] = 1;
            for (int i = 1; i < m; i++) {
                M[i][i - 1] = 1;
            }
            Matrix MM = new Matrix(M);
            Matrix.MM.mod = MOD;
            MM = MM.fastPower(n - m + 1);
            long x_n = MM.data[0][0] + MM.data[0][1] % MOD;
            out.println(x_n);
        }

    }

    static 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 int nextInt() {
            return Integer.parseInt(next());
        }

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

    }

    static class Matrix {
        public static long mod = Long.MAX_VALUE;
        public final long[][] data;
        public final int rowCount;
        public final int columnCount;

        public Matrix(int rowCount, int columnCount) {
            this.rowCount = rowCount;
            this.columnCount = columnCount;
            this.data = new long[rowCount][columnCount];
        }

        public Matrix(long[][] data) {
            this.rowCount = data.length;
            this.columnCount = data[0].length;
            this.data = data;
        }

        public static Matrix multiply(Matrix first, Matrix second) {
            Matrix result = new Matrix(first.rowCount, second.columnCount);
            for (int i = 0; i < first.rowCount; i++) {
                for (int j = 0; j < second.rowCount; j++) {
                    for (int k = 0; k < second.columnCount; k++) {
                        result.data[i][k] = (result.data[i][k] + first.data[i][j] * second.data[j][k]) % mod;
                    }
                }
            }
            return result;
        }

        public static Matrix fastMultiply(Matrix first, Matrix second) {
            Matrix result = new Matrix(first.rowCount, second.columnCount);
            for (int i = 0; i < first.rowCount; i++) {
                for (int j = 0; j < second.rowCount; j++) {
                    for (int k = 0; k < second.columnCount; k++) {
                        result.data[i][k] += first.data[i][j] * second.data[j][k];
                    }
                }
            }
            for (int i = 0; i < first.rowCount; i++) {
                for (int j = 0; j < second.columnCount; j++) {
                    result.data[i][j] %= mod;
                }
            }
            return result;
        }

        public static Matrix identityMatrix(int size) {
            Matrix result = new Matrix(size, size);
            for (int i = 0; i < size; i++) {
                result.data[i][i] = 1;
            }
            return result;
        }

        public Matrix power(long exponent) {
            if (exponent == 0) {
                return identityMatrix(rowCount);
            }
            if (exponent == 1) {
                return this;
            }
            Matrix result = power(exponent >> 1);
            result = multiply(result, result);
            if ((exponent & 1) == 1) {
                result = multiply(result, this);
            }
            return result;
        }

        public Matrix fastPower(long exponent) {
            if (exponent == 0) {
                return identityMatrix(rowCount);
            }
            if (exponent == 1) {
                return this;
            }
            Matrix result = power(exponent >> 1);
            result = fastMultiply(result, result);
            if ((exponent & 1) == 1) {
                result = fastMultiply(result, this);
            }
            return result;
        }

    }
}


import java.util.*;

public class tshirt
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        int p = sc.nextInt() - 26;
        int x = sc.nextInt();
        int y = sc.nextInt();
        int i;
        int count = 0;
        if (x%50 >= y%50)
        {
            i = (y/50)%475;
        }
        else
        {
            i = (y/50 + 1)%475;
        }
        if (x < y)
        {
            if (x%50 == y%50)
                count += (y-x)/50;
            else
                count += (y-x)/50 + 1;
        }
        for (int j = 0; j < 475; j++)
        {
            temp = i;
            for (int k = 0; k < 25; k++)
            {
                temp = (temp*96+42)%475;
                if (temp == p)
                {
                    if (count%2 == 0)
                        System.out.println(count/2);
                    else
                        System.out.println(count/2 + 1);
                    System.exit(0);
                }
            }
            i = (i+1)%475;
            count++;
        }
    }
}


import java.util.Scanner;
public class Main
{
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		int n=input.nextInt();
		int cnt=1;
		int c=0;
		while(true)
		{
			for(int i=1;i<=cnt;i++)
			{
				if(cnt%i==0)
					c++;
			}
			if(c==n)
				break;
			else
				c=0;
			cnt++;
		}
		System.out.println(cnt);
	}
}


/*
 * @author romit17
 */
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;

public class B1011{

    void solve() throws IOException {
        PrintWriter out = new PrintWriter(System.out);
        StringBuilder sb = new StringBuilder("");
        int n = ni(), m = ni();
        int[] a = na(m);
        HashMap<Integer,Integer> h = new HashMap<>();
        for(int i:a)
            h.put(i,h.getOrDefault(i, 0)+1);
        int[] b = new int[h.size()];
        int id = 0;
        for(int i:h.values())
            b[id++] = i;
        Arrays.sort(b);
        int l = 0, hi = m;
        while(l<hi)
        {
            int mid = (l+hi)/2;
            boolean tt = ispos(b,mid,n);
            if(tt && !ispos(b,mid+1,n))
            {
                System.out.println(mid);return;
            }
            if(tt)
                l = mid;
            else
                hi = mid;

        }
    }

    boolean ispos(int[] a,int k, int n)
    {
        if(k==0)
            return true;
        for(int i=a.length-1;i>=0;i--)
        {
            n -= a[i]/k;
            if(n<=0)
                return true;
        }
        return false;
    }

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

    private byte[] inbuf = new byte[1024];
    public int lenbuf = 0, ptrbuf = 0;
    InputStream is = System.in;

    private int readByte() {
        if (lenbuf == -1) {
            throw new InputMismatchException();
        }
        if (ptrbuf >= lenbuf) {
            ptrbuf = 0;
            try {
                lenbuf = is.read(inbuf);
            } catch (IOException e) {
                throw new InputMismatchException();
            }
            if (lenbuf <= 0) {
                return -1;
            }
        }
        return inbuf[ptrbuf++];
    }

    private boolean isSpaceChar(int c) {
        return !(c >= 33 && c <= 126);
    }

    private int skip() {
        int b;
        while ((b = readByte()) != -1 && isSpaceChar(b));
        return b;
    }

    private double nd() {
        return Double.parseDouble(ns());
    }

    private char nc() {
        return (char) skip();
    }

    private String ns() {
        int b = skip();
        StringBuilder sb = new StringBuilder();
        while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != ' ')
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    private char[] ns(int n) {
        char[] buf = new char[n];
        int b = skip(), p = 0;
        while (p < n && !(isSpaceChar(b))) {
            buf[p++] = (char) b;
            b = readByte();
        }
        return n == p ? buf : Arrays.copyOf(buf, p);
    }

    private char[][] nm(int n, int m) {
        char[][] map = new char[n][];
        for (int i = 0; i < n; i++) {
            map[i] = ns(m);
        }
        return map;
    }

    private int[] na(int n) {
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            a[i] = ni();
        }
        return a;
    }

    private int ni() {
        int num = 0, b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

    private long nl() {
        long num = 0;
        int b;
        boolean minus = false;
        while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
        if (b == '-') {
            minus = true;
            b = readByte();
        }

        while (true) {
            if (b >= '0' && b <= '9') {
                num = num * 10 + (b - '0');
            } else {
                return minus ? -num : num;
            }
            b = readByte();
        }
    }

}


/* package codechef; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
public class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		 Scanner ob=new Scanner(System.in);
         int n=ob.nextInt();
         int k=ob.nextInt();
         if(n==1&& k==1)
         System.out.println("0");
         else
         {
             int b[]=new int[n];
             b[0]=1;
             for(int i=2;i<=n;i++)
             {
                 b[i-1]=b[i-2]+i;
             }
             int l=0,h=n-1;
             int ans=0;
             while(l<=h)
             {
                 int mid=(l+h)/2;
                 if(b[mid-1]-(n-mid)==k)
                 {
                     ans=n-mid;
                     break;
                 }
                 else if(b[mid-1]-(n-mid)<k)
                 l=mid+1;
                 else
                 h=mid-1;
             }
             System.out.println(ans);
         }
	}
}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

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

    static class TaskA {
        static int[] p = new int[250];
        static int[] d = new int[100];
        static int n;
        static int l;
        static int k;
        static int dist;

        static boolean dfs(int time, boolean[][] vis) {
            dist++;
            if (dist == n + 1) return true;
            for (int i = time + 1; i < 2 * k + time + 1; i++) {
                if (vis[dist][i % (2 * k)]) continue;
                vis[dist][i % (2 * k)] = true;
                if (d[dist] + p[i % (2 * k)] <= l) {
                    if (dfs(i, vis)) return true;
                } else break;
            }
            dist--;
            return false;
        }

        public void solve(int testNumber, Scanner in, PrintWriter out) {
            int t = in.nextInt();
            while (t-- > 0) {
                n = in.nextInt();
                k = in.nextInt();
                l = in.nextInt();
                for (int i = 1; i <= n; i++)
                    d[i] = in.nextInt();
                for (int i = 0; i < 2 * k; i++) {
                    if (i < k) p[i] = i;
                    else p[i] = 2 * k - i;
                }
                boolean[][] vis = new boolean[110][220];
                for (int i = 0; i < 2 * k; i++) {
                    dist = 1;
                    if (d[dist] + p[i] <= l) {
                        if (dfs(i, vis)) {
                            out.println("Yes");
                            break;
                        }
                    }
                    if (i == 2 * k - 1) out.println("No");
                }
            }
        }

    }
}


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

/**
 * Created by aps36017 on 14/7/17.
 */
public class Main {
    static boolean check(char array[][]){
        for(int i = 0;i < 10;i++){
            for(int j = 0;j < 10;j++){
                int cnt = 0;
                for(int k = 0;k < 5 && j + k < 10;k++){
                    if(array[i][j + k] == 'X')++cnt;
                }
                if(cnt == 5)return true;
                cnt = 0;
                for(int k = 0;k < 5 && i + k < 10;k++){
                    if(array[i + k][j] =='X')++cnt;
                }
                if(cnt == 5)return true;
                cnt = 0;
                for(int k = 0;k < 5 && i + k < 10 && j + k < 10;k++){
                    if(array[i + k][j + k] == 'X')++cnt;
                }
                if(cnt == 5)return true;
                cnt = 0;
                for(int k = 0;k < 5 && i + k >= 0 && j - k >= 0;k++){
                    if(array[i + k][j - k] == 'X')++cnt;
                }
                if(cnt == 5)return true;
            }
        }
        return false;
    }
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st;
        char array[][] = new char[10][10];
        for(int i = 0;i < 10;i++){
            String str = br.readLine();
            for(int j = 0;j < 10;j++)
                array[i][j] = str.charAt(j);
        }
        boolean ans = false;
        for(int i = 0;i < 10;i++){
            for(int j = 0;j < 10;j++){
                if(array[i][j] != '.')continue;
                array[i][j] = 'X';
                ans |= check(array);
                array[i][j] = '.';
            }
        }
        if(ans)
        System.out.println("YES");
        else System.out.println("NO");
    }
}


import java.text.DecimalFormat;
import java.util.Scanner;

/**
 * Created by u3 on 16-4-2.
 */
public class pb {
    public static void main(String[] args) {
        int a = 0;
        String[] m, r;
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            a = scanner.nextInt();
            scanner.nextLine();
            m = scanner.nextLine().split(" ");
            r = scanner.nextLine().split(" ");
            int count = 0;
            for (int i = 0; i < 720720; i++) {
                for (int j = 0; j < m.length; j++)
                    if (i % (new Integer(m[j])) == (new Integer(r[j]))) {
                        count++;
                    }
            }
            double d = (double) count / (720720);
            DecimalFormat df1 = new DecimalFormat("0.000000");
            System.out.println(df1.format(d));
        }
    }
}


import java.util.Scanner;
mpublic class Main
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        int k = scan.nextInt();
        int a = scan.nextInt();
        int b = scan.nextInt();
        int ans =0;
        if(a/k>0 && b/k>0){ans = a/k+b/k;}
        else if(a%k==0 && a/k>=1){ans = a/k+b/k;}
        else if(b%k==0 && b/k>=1){ans = a/k+b/k;}
        else{ans = -1;}
        System.out.println(ans);
    }
}


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

public final class Main {
    private FScanner in;
    //    -------------I/O-------------    \\
    private PrintWriter out;

    public static void main(String[] args) {
        new Main().runIO();
    }

    boolean isPrime(int a) {
        if (a == 1) {
            return false;
        }
        for (int i = 2; i * i <= a; i++) {
            if (a % i == 0)
                return false;
        }
        return true;
    }

    public void solve() {
        long n = in.nextLong();
        long k = in.nextLong();
        long[] a = new long[31623000];
        if (n == 1) {
            if (k > 1) {
                out.println(-1);
                return;
            }
        }
        long i = 1;
        int j = 0;
        for (; i <= Math.sqrt(n) && k > 0; i++) {
            if (n % i == 0) {
                k--;
                a[j] = i;
                j++;
            }
        }
        if (k == 0) {
            out.println(a[j - 1]);
        } else {
            int tmp = j - (int) k;
            if (Math.sqrt(n)==(int)Math.sqrt(n)) {
                tmp--;
            }
            if (tmp >= 0 && tmp < a.length && a[tmp] != 0) {
                out.println(n / a[tmp]);
            } else {
                out.println(-1);
            }
        }
    }

    void run() {
        try {
            in = new FScanner(new File("input.txt"));
            out = new PrintWriter(new BufferedWriter(new FileWriter(new File("output.txt"))));
            solve();
            out.close();
            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    void runIO() {
        in = new FScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        solve();
        out.close();
        in.close();
    }

    class FScanner {

        private BufferedReader br;
        private StringTokenizer st;

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

        public FScanner(File f) {
            try {
                br = new BufferedReader(new FileReader(f));
            } catch (FileNotFoundException ex) {
                ex.printStackTrace();
            }
        }

        void useString(String s) {
            if (s != null) {
                st = new StringTokenizer(s);
            }
        }

        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 s = "";
            try {
                s = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return s;
        }

        void close() {
            try {
                br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}


public class Java {

    public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);
    int Y,w;
    Y=sc.nextInt();
    w=sc.nextInt();
    String arr[]={"1/6","1/3","1/2","2/3","5/6","1/1"};
    int d=Math.max(Y, w);
        System.out.println(arr[6-d]);
    }
}


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

public class D {

	static Scanner sc = new Scanner(System.in);
	static PrintWriter out = new PrintWriter(System.out);

	static void exit() {
		sc.close();
		out.close();
	}

	int mod = 1000000007;
	int a, b, k, t;
	int M;
	int c [][];
	int p [][];
	public D () {
		a = sc.nextInt();
		b = sc.nextInt();
		k = sc.nextInt();
		t = sc.nextInt();
		M = Math.abs(2*t*k + a + b+k);
		c = new int [2*t+1][2*M+1];
		p = new int [2*t+1][2*M+1];
		for (int i = 0; i < c.length; i++) Arrays.fill(c[i], 0);
		for (int i = 0; i < p.length; i++) Arrays.fill(p[i], 0);
		// solve t = 0: c[0][m] = 1 if 0 <= m
		for (int m = 0; m <= M; m++) c[0][M+m] = 1;
		p[0][M+0] = c[0][M+0];
		for (int m = 1; m <= M; m++) p[0][M+m] = p [0][M+m-1] + c[0][M+m];

		// solve for t > 0
		for (int n = 1; n <= 2*t; n++) {
			for (int m = -n*k; m <= n*k; m++) {
				c[n][M+m] = (p[n-1][M+m+k] - p[n-1][M+m-k-1])%mod;
				if (c[n][M+m]<0) c[n][M+m] += mod;
			}
			for (int m = n*k+1; M+m <= 2*M; m++) c[n][M+m] = c[n][M+m-1];
			p[n][M-n*k] = c[n][M-n*k];
			for (int m = -n*k+1; m <= M; m++) p[n][M+m] = (p[n][M+m-1] + c[n][M+m])%mod;
		}
		/*
		for (int n = 0; n <= 2; n++) {
			for (int m = -n*k; m <= n*k; m++) {
				out.println("c[" + n + "][" + m + "]=" + c[n][M+m]);
			}
		}
		*/
		out.println(c[2*t][M+a-b-1]);
		//out.println(c(2*T, a-b-1));
	}

	int c(int n, int m) {
		if (n == 0) {
			return 0<=m ? 1 : 0;
		}
		if (m < -n*k) return 0;
		if (m > n*k) return c(n, n*k);
		if (c[n][M+m] != -1) return c[n][M+m];
		c[n][M+m] = p(n-1, m+k) - p(n-1, m-k-1);
		//out.println("c["+n+"]["+m+"]=" + c[n][M+m]);
		return c[n][M+m];
	}

	int p(int n, int m) {
		if (m < -n*k) return 0;
		if (p[n][M+m] != -1) return p[n][M+m];
		p[n][M+m] = p(n, m-1) + c(n, m);
		//out.println("p["+n+"]["+m+"]=" + p[n][M+m]);
		return p[n][M+m];
	}

	public static void main(String[] args) {
		new D();
		exit();
	}
}


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

public class A {
    static class Node implements Comparable<Node>{
        int x;
        long w;
        int id;

        public Node(int x,long w,int id){
           this.x=x;
           this.w=w;
           this.id=id;
        }
        public int compareTo(Node c){
            int i=Long.compare(this.w,c.w);
          return i;

        }
        @Override
        public boolean equals(Object o){
            if(o instanceof Node){
                Node c = (Node)o;
                return true;
            }
            return false;
        }
    }

    //public static PrintWriter pw;
    public static PrintWriter pw = new PrintWriter(System.out);

    public static void solve() throws IOException {
//	pw=new PrintWriter(new FileWriter("C:\\Users\\shree\\Downloads\\small_output_in"));
        FastReader sc = new FastReader();
        int a=sc.I(); int b=sc.I();  k=sc.I(); int n=sc.I();
        int MX=200001;
        dp1=new long[n+1][2*MX+1];
        dp2=new long[n+1][2*MX+1];

        dp1[0][a+MX]=1;
        dp2[0][b+MX]=1;
        int mx=Math.max(a,b)+k*n+MX;
        int mn=Math.min(a,b)-k*n+MX;

        for(int i=1;i<=n;i++) {
             for(int j=mn;j<=mx;j++){

                     dp1[i][j - k] += dp1[i - 1][j];
                     dp1[i][j + k + 1] -= dp1[i - 1][j];

                     dp2[i][j - k] += dp2[i - 1][j];
                     dp2[i][j + k + 1] -= dp2[i - 1][j];

             }
             for(int j=mn;j<=mx;j++){
                 dp1[i][j]=(dp1[i][j]+dp1[i][j-1])%M;
                 dp2[i][j]=(dp2[i][j]+dp2[i][j-1])%M;
             }

        }
        long ans=0;

        long sum2[]=new long[2*MX+1];

        for(int i=mn;i<=mx;i++){
            sum2[i]=(sum2[i-1]+dp2[n][i])%M;
        }
        for(int i=mn;i<=mx;i++){
            ans=(ans+(dp1[n][i]*sum2[i-1])%M)%M;
          //  pw.println(dp1[n][i]+" "+sum2[i-1]);

        }
        pw.println(ans);

        pw.close();

    }
    static long dp1[][];
    static long dp2[][];
    static int k;

    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                try {

                    solve();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, "1", 1 << 26).start();

    }

    static BufferedReader br;
    static long M = (long) 1e9 + 7;

    static class FastReader {

        StringTokenizer st;

        public FastReader() throws FileNotFoundException {
            //br=new BufferedReader(new FileReader("C:\\Users\\shree\\Downloads\\B-small-practice.in"));
            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 I() {
            return Integer.parseInt(next());
        }

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

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

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

        public boolean hasNext() throws IOException {
            while (st == null || !st.hasMoreTokens()) {
                String s = br.readLine();
                if (s == null) {
                    return false;
                }
                st = new StringTokenizer(s);
            }
            return true;
        }

    }
}