import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Locale;
import java.util.StringTokenizer;

public class Main {

    long binpow(int a, int n) {
        long r = 1;
        while (n > 0) {
            if ((n & 1) > 0) {
                r *= a;
            }
            a *= a;
            n /= 2;
        }
        return r;
    }

    long gcd(long x, long y) {
        return y == 0 ? x : gcd(y, x % y);
    }

    void solve() throws IOException {
        BigInteger n = BigInteger.valueOf(readLong()), k = BigInteger.valueOf(readLong()), m = BigInteger.valueOf(readLong()), d = BigInteger.valueOf(readLong());
        long maxRes = -1;

        // todo check M

        BigInteger resForM = checkM(n.longValue(), k.longValue(), m.longValue(), d.longValue());
        System.err.println("resForM = " + resForM);
        maxRes = Math.max(maxRes, resForM.longValue());

        for (long rounds = 1; rounds <= d.longValue(); rounds++) {
            System.err.println("rounds = " + rounds);
            BigInteger x = n.divide(k.multiply(BigInteger.valueOf(rounds - 1)).add(BigInteger.ONE));
            System.err.println("x = " + x);
            if (x.compareTo(m) > 0) {
                continue;
            }
            BigInteger res = x.multiply(BigInteger.valueOf(rounds));
            System.err.println("res = " + res);
            if (res.longValue() > maxRes) {
                maxRes = res.longValue();
            }
        }
        System.out.println(maxRes);
    }

    private BigInteger checkM(long n, long k, long m, long d) {
        long s = n / m;
        long rounds = (s + k - 1) / k;
        if (rounds > d) {
            return BigInteger.valueOf(-1);
        }
        return BigInteger.valueOf(rounds).multiply(BigInteger.valueOf(m));
    }

    //-------------------------------------------------

    final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;

    BufferedReader in;
    PrintWriter out;
    StringTokenizer tok;

    public void run() {
        try {
            long startTime = System.currentTimeMillis();
            Locale.setDefault(Locale.US);
            if (ONLINE_JUDGE) {
                in = new BufferedReader(new InputStreamReader(System.in));
                out = new PrintWriter(System.out);
            } else {
                in = new BufferedReader(new FileReader("input.txt"));
                out = new PrintWriter("output.txt");
            }
            tok = new StringTokenizer("");
            solve();
            in.close();
            out.close();
            long endTime = System.currentTimeMillis();
            long totalMemory = Runtime.getRuntime().totalMemory();
            long freeMemory = Runtime.getRuntime().freeMemory();
            System.err.println("Time = " + (endTime - startTime) + " ms");
            System.err.println("Memory = " + ((totalMemory - freeMemory) / 1024) + " KB");
        } catch (Throwable e) {
            e.printStackTrace(System.err);
            System.exit(-1);
        }
    }

    String readString() {
        while (!tok.hasMoreTokens()) {
            String line = null;
            try {
                line = in.readLine();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            if (line == null) return null;
            tok = new StringTokenizer(line);
        }
        return tok.nextToken();
    }

    int readInt() {
        return Integer.parseInt(readString());
    }

    long readLong() throws IOException {
        return Long.parseLong(readString());
    }

    double readDouble() throws IOException {
        return Double.parseDouble(readString());
    }

    void debug(Object... o) {
        if (!ONLINE_JUDGE) {
            System.err.println(Arrays.deepToString(o));
        }
    }

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


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

public class Prob242A {
	public static void main(String[] Args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());
		int x = Integer.parseInt(st.nextToken());
		int m = Integer.parseInt(st.nextToken());

		BigInteger temp = BigInteger.ONE;
		BigInteger mod = BigInteger.valueOf(1000000009);
		BigInteger two = temp.add(temp);
		for (int i = 0; i < m; i++)
			temp = temp.multiply(two).mod(mod);
		temp = temp.subtract(BigInteger.ONE);

		BigInteger bi = BigInteger.ONE;
		int temp2 = temp.intValue();
		for (int i = temp2, j = 0; j < x; i--, j++) {
			bi = bi.multiply(BigInteger.valueOf(i)).mod(mod);
			if (bi.intValue() == 0)
				break;
		}

		System.out.println(bi);

	}
}


import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String s = in.next();
		int n  = s.length();
		char[] a = new char[n];
		for(int i = 0; i < s.length(); ++i) {
			a[i] = s.charAt(i);
		}

		int y=0,g=0,r=0,b=0;
		int[] YBRG = {-1,-1,-1,-1};
		char[] cYBRG = {'Y','B','R','G'};
		for(int i = 0;  i < s.length(); i++) {
			if(a[i] == 'Y' && YBRG[0] == -1) YBRG[0] = i%4;
			if(a[i] == 'B' && YBRG[1] == -1) YBRG[1] = i%4;
			if(a[i] == 'R' && YBRG[2] == -1) YBRG[2] = i%4;
			if(a[i] == 'G' && YBRG[3] == -1) YBRG[3] = i%4;
		}
		for(int z = 0; z < 4; ++z) {
			for(int i = 0; i < 3; ++i) {
				if(YBRG[i] > YBRG[i+1]) {
					int t = YBRG[i];
					char c = cYBRG[i];
					YBRG[i] = YBRG[i+1];
					cYBRG[i] = cYBRG[i+1];
					YBRG[i+1] = t;
					cYBRG[i+1] = c;
				}
			}
		}

		for(int i = 0; i < s.length(); ++i) {
			if(s.charAt(i) == '!') {
				if(cYBRG[i%4] == 'Y') y++;
				if(cYBRG[i%4] == 'B') b++;
				if(cYBRG[i%4] == 'R') r++;
				if(cYBRG[i%4] == 'G') g++;
			}
		}

		System.out.println(r + " " + b + " " + y + " " + g);
	}
}


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

/**
 * @author khokharnikunj8
 */

public class Main {
    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Main().solve();
            }
        }, "1", 1 << 26).start();
    }

    void solve() {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        BNumbers solver = new BNumbers();
        solver.solve(1, in, out);
        out.close();
    }

    static class BNumbers {
        int mod = 1000000007;

        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            int n = in.scanInt();
            int[] ar = new int[10];
            for (int i = 0; i < 10; i++) {
                ar[i] = in.scanInt();
            }
            int[][] nCr = new int[101][];
            for (int i = 0; i <= 100; i++) {
                nCr[i] = new int[i + 2];
                for (int j = 0; j <= i; j++) {
                    if (i == 0 || j == 0) nCr[i][j] = 1;
                    else nCr[i][j] = (nCr[i - 1][j - 1] + nCr[i - 1][j]) % mod;
                }
            }
            long ans = 0;
            for (int i = 1; i <= 9; i++) {
                ar[i]--;
                long[][] dp = new long[2][n];
                dp[1][0] = 1;
                for (int j = 0; j <= 9; j++) {
                    Arrays.fill(dp[j & 1], 0);
                    for (int k = 0; k < n; k++) {
                        for (int l = Math.max(0, ar[j]); k + l < n; l++) {
                            dp[j & 1][k + l] = (dp[j & 1][k + l] + dp[(j - 1) & 1][k] * nCr[k + l][l]) % mod;
                        }
                    }
                }
                for (int j = 0; j < n; j++) ans = (ans + dp[1][j]) % mod;
                ar[i]++;
            }
            out.println(ans);

        }

    }

    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 int scanInt() {
            int integer = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    integer *= 10;
                    integer += n - '0';
                    n = scan();
                }
            }
            return neg * integer;
        }

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

    }
}


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

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

	static class F {
		private Long[][] lastX = new Long[2010][2010];
		private long mod = 998244353;
		private long p = 0;
		private long[] pPow = new long[2010];
		private long[] qPow = new long[2010];
		private long[] strong = new long[2010];
		private Long[] dp = new Long[2010];

		public void solve(int testNumber, InputReader in, PrintWriter out) {
			int n = in.nextInt();
			int a = in.nextInt(), b = in.nextInt();
			p = MathUtils.powMod(b, mod - 2, mod) * a % mod;
			pPow[0] = 1;
			qPow[0] = 1;
			for (int i = 1; i < 2010; ++i) {
				pPow[i] = pPow[i - 1] * p % mod;
				qPow[i] = qPow[i - 1] * (1 - p + mod) % mod;
			}
			strong[1] = 1;
			for (int i = 2; i <= n; ++i) {
				long ans = 0;
				for (int j = 1; j < i; ++j) {
					ans += strong[j] * computeLastX(i, j) % mod;
					if (ans >= mod) ans -= mod;
				}
				strong[i] = 1 - ans;
				if (strong[i] < 0) strong[i] += mod;
			}
			long ans = dp(n);
			out.println(ans);
		}

		private long computeLastX(int n, int i) {
			if (lastX[n][i] != null) return lastX[n][i];
			if (n == i || i == 0) return lastX[n][i] = 1L;
			long ans = qPow[i] * computeLastX(n - 1, i) % mod + pPow[n - i] * computeLastX(n - 1, i - 1) % mod;
			return lastX[n][i] = ans % mod;
		}

		private long dp(int n) {
			if (dp[n] != null) return dp[n];
			if (n < 2) return dp[n] = 0L;
			if (n < 3) return dp[n] = 1L;
			long ans = strong[n] * (n * (n - 1) / 2) % mod;
			for (int x = 1; x < n; ++x) {
				long tmp = computeLastX(n, x) * strong[x] % mod;
				tmp *= (x * (x - 1) / 2 + x * (n - x) + dp(n - x) + dp(x)) % mod;
				ans += tmp % mod;
			}
			ans %= mod;
			ans *= MathUtils.powMod(1 + mod - strong[n], mod - 2, mod);
			return dp[n] = ans % mod;
		}

	}

	static class InputReader {
		public BufferedReader reader;
		public StringTokenizer tokenizer;

		public InputReader(InputStream inputStream) {
			reader = new BufferedReader(new InputStreamReader(inputStream));
			tokenizer = null;
		}

		public InputReader(String inputFile) throws FileNotFoundException {
			reader = new BufferedReader(new FileReader(inputFile));
			tokenizer = null;
		}

		public String nextLine() {
			String curr = "";
			try {
				curr = reader.readLine();
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
			return curr;
		}

		public String nextString() {
			while (tokenizer == null || !tokenizer.hasMoreTokens()) {
				try {
					tokenizer = new StringTokenizer(nextLine());
				} catch (Exception e) {
					throw new RuntimeException(e);
				}
			}
			return tokenizer.nextToken();
		}

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

	}

	static class MathUtils {
		public static long powMod(long a, long b, long m) {
			long ans = 1;
			while (b > 0) {
				if (b % 2 == 1) ans = ans * a % m;
				b >>= 1;
				a = a * a % m;
			}
			return ans % m;
		}

	}
}


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;

/**
 * @author khokharnikunj8
 */

public class Main {
    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Main().solve();
            }
        }, "1", 1 << 26).start();
    }

    void solve() {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        DFelicitysBigSecretRevealed solver = new DFelicitysBigSecretRevealed();
        solver.solve(1, in, out);
        out.close();
    }

    static class DFelicitysBigSecretRevealed {
        int mod = 1000000007;

        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            int n = in.scanInt();
            char[] ar = in.scanChars(n);
            int[][] dp = new int[n + 1][1 << 20];
            long ans = 0;
            for (int i = 0; i < n; i++) {
                dp[i][0]++;
                for (int j = 0; j < (1 << 20); j++) {
                    if (dp[i][j] == 0) continue;
                    for (int k = i + 1, val = 0; k <= n && val <= 20; k++) {
                        val <<= 1;
                        val |= (ar[k - 1] - '0');
                        if (val > 20) break;
                        if ((j | (1 << (val - 1))) < (1 << 20)) {
                            if (val != 0)
                                dp[k][(j | (1 << (val - 1)))] = (dp[k][(j | (1 << (val - 1)))] + dp[i][j]) % mod;
                        }
                    }
                }
                for (int j = 1; j <= 20; j++) ans = (ans + dp[i + 1][(1 << j) - 1]) % mod;
            }
            out.println(ans);

        }

    }

    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 int scanInt() {
            int integer = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    integer *= 10;
                    integer += n - '0';
                    n = scan();
                }
            }
            return neg * integer;
        }

        public char[] scanChars(int n) {
            char[] ar = new char[n];
            int c = scan();
            boolean flag = true;
            while (isWhiteSpace(c)) c = scan();
            ar[0] = (char) c;
            for (int i = 1; i < n; i++) ar[i] = (char) (c = scan());
            return ar;
        }

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

    }
}


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;

public class E3 {

    void solve() throws IOException {
        char[] c = reader.readLine().toCharArray();
        int[] a = doit(c);
        if (a != null) {
            out.println(a.length);
            for (int j = 0; j < a.length; j++) {
                out.print(a[j] + " ");
            }
        }
    }

    int[] doit(char[] c) {
        int l = 0;
        int r = 200000;
        while (l < r) {
            index = 0;
            int n = (l + r) / 2;
            int[] a = new int[n];
            int[] b = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = b[i] = i + 1;
            }
            boolean res = simulateMerge(0, n, c, a, b);
            if (!res) {
                r = n;
            } else if (index < c.length) {
                l = n + 1;
            } else {
                for (int i = 0; i < n; i++) {
                    b[a[i] - 1] = i + 1;
                }
                return b;
            }
        }
        return null;
    }

    int index = 0;

    boolean simulateMerge(int l, int r, char[] c, int[] a, int[] b) {
        if (r - l <= 1) return true;
        int m = (l + r) >> 1;
        simulateMerge(l, m, c, a, b);
        simulateMerge(m, r, c, a, b);
        int i = l;
        int j = m;
        int k = l;
        while (i < m && j < r) {
            if (index == c.length) {
                return false;
            }
            if (c[index] == '0') {
                b[k] = a[i];
                i++;
            } else {
                b[k] = a[j];
                j++;
            }
            index++;
            k++;
        }

        while (i < m) {
            b[k] = a[i];
            i++;
            k++;
        }
        while (j < r) {
            b[k] = a[j];
            j++;
            k++;
        }
        for (int x = l; x < r; x++) {
            a[x] = b[x];
        }
        return true;
    }

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

    void run() throws IOException {
        reader = new BufferedReader(new InputStreamReader(System.in));
//		reader = new BufferedReader(new FileReader("input.txt"));
        tokenizer = null;
        out = new PrintWriter(new OutputStreamWriter(System.out));
//		out = new PrintWriter(new FileWriter("output.txt"));
        solve();
        reader.close();
        out.flush();

    }

    BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter out;

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

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

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

    int[] nextIntArray(int n) throws IOException {
        int[] result = new int[n];
        for (int i = 0; i < n; i++) {
            result[i] = nextInt();
        }
        return result;
    }

    long[] nextLongArray(int n) throws IOException {
        long[] result = new long[n];
        for (int i = 0; i < n; i++) {
            result[i] = nextLong();
        }
        return result;
    }

    double[] nextDoubleArray(int n) throws IOException {
        double[] result = new double[n];
        for (int i = 0; i < n; i++) {
            result[i] = nextDouble();
        }
        return result;
    }

    int[][] nextIntArray(int n, int m) throws IOException {
        int[][] result = new int[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                result[i][j] = nextInt();
            }
        }
        return result;
    }

    long[][] nextLongArray(int n, int m) throws IOException {
        long[][] result = new long[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                result[i][j] = nextLong();
            }
        }
        return result;
    }

    double[][] nextDoubleArray(int n, int m) throws IOException {
        double[][] result = new double[n][m];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                result[i][j] = nextDouble();
            }
        }
        return result;
    }

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


import java.util.Scanner;
public class r262B {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner scnr = new Scanner(System.in);

		int a = scnr.nextInt();
		int b = scnr.nextInt();
		int c = scnr.nextInt();
		int count = 0;
		long[] ans = new long[81];
		scnr.close();

		for (int i = 1; i <= 81; i++){
			long answer = 1;
			for (int j = 0; j < a; j++)
				answer = answer * i;
			answer = answer*b+c;
			if (answer > 0 && answer < 1000000000 && getSum((int)answer) == i){
				ans[count] = answer;
				count++;
			}
		}

		System.out.print(count);
		if (count!= 0){
			System.out.println();
			for (int i = 0; i < count-1; i++){
				System.out.print(ans[i] + " ");
			}
			System.out.print(ans[count-1]);
		}

	}

	private static int getSum(int a){
		int b = 0;
		while (a > 0){
			b = a%10 + b;
			a = a/10;
		}
		return b;
	}
}


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

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

File input,output;
Scanner in;
PrintWriter out;

public void run() throws IOException
{
in = new Scanner(System.in);
out = new PrintWriter(System.out);

int vx = in.nextInt();
int vy = in.nextInt();
int px = in.nextInt();
int py = in.nextInt();
int wx1 = in.nextInt();
int wy1 = in.nextInt();
int wx2 = in.nextInt();
int wy2 = in.nextInt();
int mx1 = in.nextInt();
int my1 = in.nextInt();
int mx2 = in.nextInt();
int my2 = in.nextInt();

Line2D vp = new Line2D.Double(vx,vy,px,py);
Line2D wall = new Line2D.Double(wx1,wy1,wx2,wy2);
Line2D mirror = new Line2D.Double(mx1,my1,mx2,my2);

if(!wall.intersectsLine(vp))
    {
//  out.println("Wall doesn't intersect vp");
    int vpx = px-vx;
    int vpy = py-vy;
    int mirrorX = mx2-mx1;
    int mirrorY = my2-my1;

//  out.println("Vasya-Petya vector: "+vpx+" "+vpy);
//  out.println("Mirror vector: "+mirrorX+" "+mirrorY);

    if(vpx*mirrorY-vpy*mirrorX==0)
        {
        out.println("YES");
        out.close();
        return;
        }

    if(!mirror.intersectsLine(vp))
        {
        out.println("YES");
        out.close();
        return;
        }
    }
//else out.println("Wall intersects vp");

int mirrorX = mx2-mx1;
int mirrorY = my2-my1;

int normalX = -mirrorY;
int normalY = mirrorX;

int ma = normalX;
int mb = normalY;
int mc = -ma*mx1-mb*my1;

int signv = ma*vx+mb*vy+mc;
int signp = ma*px+mb*py+mc;

signv = signv==0 ? 0 : signv/Math.abs(signv);
signp = signp==0 ? 0 : signp/Math.abs(signp);

if(signv*signp<=0)
    {
    out.println("NO");
    out.close();
    return;
    }

double pdist = mirror.ptLineDist(px,py);
double normalLength = Math.sqrt(normalX*normalX+normalY*normalY);
double normX = normalX*pdist/normalLength;
double normY = normalY*pdist/normalLength;

double p1X = px+2*normX;
double p2X = px-2*normX;
double p1Y = py+2*normY;
double p2Y = py-2*normY;

double p1dist = mirror.ptLineDist(p1X,p1Y);
double p2dist = mirror.ptLineDist(p2X,p2Y);
double qX,qY;
if(p1dist<p2dist)
    {
    qX = p1X;
    qY = p1Y;
    }
else
    {
    qX = p2X;
    qY = p2Y;
    }

Line2D imaginaryLine = new Line2D.Double(vx,vy,qX,qY);
if(!mirror.intersectsLine(imaginaryLine))
    {
    out.println("NO");
    out.close();
    return;
    }

double lx = vx;
double ly = vy;
double rx = qX;
double ry = qY;
double medx,medy;

while(Math.sqrt((lx-rx)*(lx-rx)+(ly-ry)*(ly-ry))>1e-4)
    {
    medx = (lx+rx)/2;
    medy = (ly+ry)/2;
    if(mirror.relativeCCW(medx,medy)==mirror.relativeCCW(vx,vy))
        {
        lx = medx;
        ly = medy;
        }
    else
        {
        rx = medx;
        ry = medy;
        }
    }

medx = (lx+rx)/2;
medy = (ly+ry)/2;

Line2D vmirror = new Line2D.Double(vx,vy,medx,medy);
Line2D pmirror = new Line2D.Double(px,py,medx,medy);
if(wall.intersectsLine(vmirror)||wall.intersectsLine(pmirror)) out.println("NO");
else out.println("YES");
out.close();

}

}


import java.util.Scanner;

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

        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m = in.nextInt();

        int[] arr = new int[n];

        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }

        int index = -1;

        for (;;) {
            for (int j = 0; j < n; j++)

                if (arr[j] > 0) {
                    arr[j] -= m;
                    index = j;
                }

            int count = 0;

            for (int j = 0; j < n; j++)
                if (arr[j] > 0)
                    count++;

            if (count == 0)
                break;
        }

        System.out.println(index + 1);

    }
}


import java.io.IOException;
import java.util.Scanner;
public class magicnumber {
    public static void main(String[] args) throws IOException {
     Scanner cs= new Scanner(System.in);

     String s;

    s=cs.next();
       if(s.charAt(0)!='1')
       { System.out.println("NO");}
      else if(s.contains("444"))
      {  System.out.println("NO");}

      else if(s.contains("0")||s.contains("2") ||s.contains("3")||s.contains("5")||s.contains("6")||s.contains("7")|| s.contains("8")||s.contains("9"))
      {System.out.println("NO");
      }
      else
      {  System.out.println("YES");}
      }
     }


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.*;

public class B
{
	BufferedReader in;
	PrintStream out;
	StringTokenizer tok;
	public B() throws NumberFormatException, IOException
	{
		in = new BufferedReader(new InputStreamReader(System.in));
		//in = new BufferedReader(new FileReader("in.txt"));
		out = System.out;
		run();
	}
	double dist(int x1, int y1, int x2, int y2)
	{
		return Math.sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1));
	}
	boolean circInside(double d, int r1, int r2)
	{
		int min = Math.min(r2, r1);
		int max = Math.max(r1, r2);
		return (r1 < r2) && (d<max) && (min<= (max-d));
	}
	boolean circOut(double d, int r1, int r2)
	{
		return d>=r1 && d>=r2 && (d-r1)>=r2;
	}
	void run() throws NumberFormatException, IOException
	{
		int x1 = nextInt(),y1 = nextInt(),r1 = nextInt(),R1 = nextInt();
		int x2 = nextInt(),y2 = nextInt(),r2 = nextInt(),R2 = nextInt();
		int ans = 0;

		double d = dist(x1,y1,x2,y2);
		//r1
		if(circInside(d,r1,r2)||circOut(d,r1,R2) || circInside(d,R2,r1))
			ans++;
		//r2
		if(circInside(d,r2,r1) || circOut(d,r2,R1) ||circInside(d,R1,r2))
			ans++;
		//R1
		if(circInside(d,R1,R2)||circInside(d,R1,r2)||circOut(d,R1,R2) || circInside(d,R2,r1))
			ans++;
		//R2
		if(circInside(d,R2,R1) || circInside(d,R2,r1)||circOut(d,R1,R2) || circInside(d,R1,r2))
			ans++;
		out.println(ans++);

	}
	public static void main(String[] args) throws NumberFormatException, IOException
	{
		new B();
	}
	String nextToken() throws IOException
	{
		if(tok ==null || !tok.hasMoreTokens()) tok = new StringTokenizer(in.readLine());
		return tok.nextToken();
	}
	int nextInt() throws NumberFormatException, IOException
	{
		return Integer.parseInt(nextToken());
	}
	long nextLong() throws NumberFormatException, IOException
	{
		return Long.parseLong(nextToken());
	}
	double nextDouble() throws NumberFormatException, IOException
	{
		return Double.parseDouble(nextToken());
	}
}


import java.io.BufferedReader;
// import java.io.FileInputStream;
// import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

import static java.lang.Math.*;
import static java.util.Arrays.fill;
import static java.util.Arrays.sort;

public class Main {
    FastScanner in;
    PrintWriter out;

    class Pair {
        int s, d, c, id;

        Pair(int s, int d, int c, int id) {
            this.s = s;
            this.d = d;
            this.c = c;
            this.id = id;
        }
    }

    private void solve() throws IOException {
        int n = in.nextInt(), m = in.nextInt();
        PriorityQueue<Pair> q = new PriorityQueue<>(Comparator.comparingInt(o -> o.d));
        Pair[] p = new Pair[m];
        boolean[] block = new boolean[n + 1];
        for (int i = 0; i < m; i++) {
            p[i] = new Pair(in.nextInt(), in.nextInt(), in.nextInt(), i);
            block[p[i].d] = true;
        }
        shuffle(p);
        sort(p, Comparator.comparingInt(o -> o.s));
        int[] ans = new int[n];
        for (int i = 1, j = 0; i <= n; i++) {
            if (!q.isEmpty() && q.peek().d <= i && q.peek().c > 0) {
                out.print(-1);
                return;
            }
            while (j < m && p[j].s == i) {
                q.add(p[j++]);
            }

            if (!block[i]) {
                Pair c = q.peek() == null ? new Pair(0, 0, 0, -1) : q.remove();
                c.c--;
                if (c.c > 0)
                    q.add(c);
                ans[i - 1] = c.id;
            } else
                ans[i - 1] = m;
        }
        for (int i : ans)
            out.print(i + 1 + " ");
    }

    void shuffle(Pair[] a) {
        Pair b;
        Random r = new Random();
        for (int i = a.length - 1, j; i > 0; i--) {
            j = r.nextInt(i + 1);
            b = a[j];
            a[j] = a[i];
            a[i] = b;
        }
    }

    class FastScanner {
        StringTokenizer st;
        BufferedReader br;

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

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

        boolean hasNext() throws IOException {
            return br.ready() || (st != null && st.hasMoreTokens());
        }

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

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

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

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

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

    }

    private void run() throws IOException {
        in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
        out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));

        solve();

        out.flush();
        out.close();
    }

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


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

public class Main {

	static class Task {

		int NN = 200005;
		int MOD = 1000000007;
		int INF = 2000000000;
		long INFINITY = 2000000000000000000L;

		long cur;
		long sumLike=0, sumHate=0;
		int n, m;

		long [][][][] dp;

		long expo(long x, long y) {
			long ret = 1, sq =x;
			while(y>0) {
				if(y%2!=0) {
					ret = (ret * sq) % 998244353;
				}
				sq = (sq * sq) % 998244353;
				y >>= 1;
			}
			return ret;
		}

		long invMod(long p, long q) {
			return (p * expo(q, 998244353 - 2))%998244353;
		}

		long rec(int c1, int c2, int c, int flag) {
			int mul = flag==0?-1:1;
			if(dp[c1][c2][c][flag] != -1)
				return dp[c1][c2][c][flag];
			if(c==m) {
				return dp[c1][c2][c][flag] = cur + mul*c1;
			}
			long p = (cur + mul*c1);
			long q;
			if(mul == 1)
				q = (sumLike + c2 + sumHate - (c-c2));
			else
				q = (sumHate - c2 + sumLike + (c-c2));
			long ret = (rec(c1+1, c2+1, c+1, flag)*invMod(p, q))%998244353;
			if(mul == 1)
				p = (sumLike + c2) - (cur + c1);
			else
				p = (sumHate - c2) - (cur - c1);
			ret = (ret + rec(c1, c2+1, c+1, flag)*invMod(p, q))%998244353;
			if(mul == 1)
				p = (sumHate - (c-c2));
			else
				p = sumLike + (c-c2);
			ret = (ret + rec(c1, c2, c+1, flag)*invMod(p, q))%998244353;
			return dp[c1][c2][c][flag] = ret;
		}

		public void solve(InputReader in, PrintWriter out) {
			n = in.nextInt();
			m = in.nextInt();
			int [] a = new int[n];
			for(int i=0;i<n;++i)
				a[i] = in.nextInt();
			int [] w = new int[n];
			for(int i=0;i<n;++i) {
				w[i] = in.nextInt();
				if(a[i] == 0) {
					sumHate += w[i];
				} else {
					sumLike += w[i];
				}
			}
			dp = new long[m+1][m+1][m+1][2];
			for(int i=0;i<n;++i) {
				cur=w[i];
				for(int c1=0;c1<=m;++c1) {
					for(int c2=0;c2<=m;++c2) {
						for(int c=0;c<=m;++c) {
							dp[c1][c2][c][0] = dp[c1][c2][c][1] = -1;
						}
					}
				}
				out.println(rec(0, 0, 0, a[i]));
			}
		}

	}

	static void prepareIO(boolean isFileIO) {
		//long t1 = System.currentTimeMillis();
		Task solver = new Task();
		// Standard IO
		if(!isFileIO) {
			InputStream inputStream = System.in;
	        OutputStream outputStream = System.out;
	        InputReader in = new InputReader(inputStream);
	        PrintWriter out = new PrintWriter(outputStream);
	        solver.solve(in, out);
	        //out.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0);
	        out.close();
		}
        // File IO
		else {
			String IPfilePath = System.getProperty("user.home") + "/Downloads/ip.in";
	        String OPfilePath = System.getProperty("user.home") + "/Downloads/op.out";
	        InputReader fin = new InputReader(IPfilePath);
	        PrintWriter fout = null;
	        try {
				fout = new PrintWriter(new File(OPfilePath));
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
	        solver.solve(fin, fout);
	        //fout.println("time(s): " + (1.0*(System.currentTimeMillis()-t1))/1000.0);
	        fout.close();
		}
	}

	public static void main(String[] args) {
        prepareIO(false);
	}

	static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

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

        public InputReader(String filePath) {
        	File file = new File(filePath);
            try {
				reader = new BufferedReader(new FileReader(file));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            tokenizer = null;
        }

        public String nextLine() {
        	String str = "";
        	try {
				str = reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        	return str;
        }

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

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

    }

}


import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.InputStreamReader;
import java.io.BufferedOutputStream;
import java.util.StringTokenizer;
import java.io.Closeable;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.Flushable;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 */
public class Main {
	static class TaskAdapter implements Runnable {
		@Override
		public void run() {
			InputStream inputStream = System.in;
			OutputStream outputStream = System.out;
			Input in = new Input(inputStream);
			Output out = new Output(outputStream);
			BPerfectNumber solver = new BPerfectNumber();
			solver.solve(1, in, out);
			out.close();
		}
	}

	public static void main(String[] args) throws Exception {
		Thread thread = new Thread(null, new TaskAdapter(), "", 1<<29);
		thread.start();
		thread.join();
	}
	static class BPerfectNumber {
		public BPerfectNumber() {
		}

		public boolean perfect(int x) {
			String s = String.valueOf(x);
			int sum = 0;
			for(int i = 0; i<s.length(); i++) {
				sum += s.charAt(i)-'0';
			}
			return sum==10;
		}

		public void solve(int testNumber, Input in, Output pw) {
			int n = in.nextInt(), j = 0, i;
			for(i = 1; j<n; i++) {
				if(perfect(i)) {
					j++;
				}
			}
			pw.println(i-1);
		}

	}

	static class Output implements Closeable, Flushable {
		public StringBuilder sb;
		public OutputStream os;
		public int BUFFER_SIZE;
		public boolean autoFlush;
		public String LineSeparator;

		public Output(OutputStream os) {
			this(os, 1<<16);
		}

		public Output(OutputStream os, int bs) {
			BUFFER_SIZE = bs;
			sb = new StringBuilder(BUFFER_SIZE);
			this.os = new BufferedOutputStream(os, 1<<17);
			autoFlush = false;
			LineSeparator = System.lineSeparator();
		}

		public void println(int i) {
			println(String.valueOf(i));
		}

		public void println(String s) {
			sb.append(s);
			println();
			if(autoFlush||sb.length()>BUFFER_SIZE >> 1) {
				flush();
			}
		}

		public void println() {
			sb.append(LineSeparator);
		}

		public void flush() {
			try {
				os.write(sb.toString().getBytes());
			}catch(IOException e) {
				e.printStackTrace();
			}
			sb = new StringBuilder(BUFFER_SIZE);
		}

		public void close() {
			flush();
			try {
				os.close();
			}catch(IOException e) {
				e.printStackTrace();
			}
		}

	}

	static class Input {
		BufferedReader br;
		StringTokenizer st;

		public Input(InputStream is) {
			this(is, 1<<20);
		}

		public Input(InputStream is, int bs) {
			br = new BufferedReader(new InputStreamReader(is), bs);
			st = null;
		}

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

		public String next() {
			if(!hasNext()) {
				throw new InputMismatchException();
			}
			return st.nextToken();
		}

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

	}
}


import java.util.Scanner;

public class CF476A {
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		int stairsToClimb = in.nextInt();
		int multiple = in.nextInt();
		in.nextLine();
		int maxPossibleSteps = stairsToClimb;
		int minPossibleSteps = (int) Math.ceil(stairsToClimb/2.0);
		//System.out.println(minPossibleSteps);
		int toPrint = -1;

		for(int i = minPossibleSteps; i <= maxPossibleSteps; i++){
			if(i % multiple == 0){
				toPrint = i;
				break;
			}
		}
		System.out.println(toPrint);
	}
}


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

public class Forces_579_A {
	public static void main(String args[]) throws NumberFormatException, IOException
	{
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		PrintWriter pr = new PrintWriter(System.out);
		long x=Long.parseLong(br.readLine());
		int c=1;
		while(x!=1)
		{
			if(x%2==0) x=x/2;
			else
			{
				c++;
				x=x-1;
			}
		}

		pr.println(c);
		pr.close();
	}
}


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

public class D implements Runnable {

	private void Solution() throws IOException {
		long n = nextLong(), m = nextLong(), k = nextLong();
		long MOD = 1000000000 + 7, ans = 0;
		if (k > n) {
			ans = 1;
			for (int i = 0; i < n; i++) {
				ans *= m;
				ans %= MOD;
			}
		} else if (k == n) {
			ans = 1;
			for (int i = 0; i < (n + 1) / 2; i++) {
				ans *= m;
				ans %= MOD;
			}
		} else if (k == 1) {
			ans = 1;
			for (int i = 0; i < n; i++) {
				ans *= m;
				ans %= MOD;
			}
		} else if (k % 2 == 0) {
			ans = m;
		} else {
			ans = m * m;
		}
		print(ans);
	}

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

	BufferedReader in;
	PrintWriter out;
	StringTokenizer tokenizer;

	public void run() {
		try {
			in = new BufferedReader(new InputStreamReader(System.in));
			out = new PrintWriter(System.out);
			Solution();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
			System.exit(0);
		}
	}

	void print(Object... o) {
		for (int i = 0; i < o.length; i++) {
			if (i != 0)
				out.print(" ");
			out.print(o[i]);
		}
	}

	void println(Object... o) {
		for (int i = 0; i < o.length; i++) {
			if (i != 0)
				out.print(" ");
			out.print(o[i]);
		}
		out.println();
	}

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

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

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

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

	String nextToken() throws IOException {
		while (tokenizer == null || !tokenizer.hasMoreTokens())
			tokenizer = new StringTokenizer(nextLine());
		return tokenizer.nextToken();
	}
}


import java.io.*;
import java.util.*;
import java.lang.*;
import java.util.HashMap;
    public class templ {
    int binarySearch(int arr[], int l, int r, int x)
    {
        if (r >= l)
        {
            int mid = l + (r - l)/2;
            if (arr[mid] == x)
            return mid;
            if (arr[mid] > x)
            return binarySearch(arr, l, mid-1, x);
            return binarySearch(arr, mid+1, r, x);
        }
        return -1;
    }
    void merge1(int arr[], int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        for (int i=0; i<n1; ++i)
            L[i] = arr[l + i];
        for (int j=0; j<n2; ++j)
            R[j] = arr[m + 1+ j];
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            j++;
            k++;
        }
    }
    void sort1(int arr[], int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort1(arr, l, m);
            sort1(arr , m+1, r);
            merge1(arr, l, m, r);
        }
    }
    void merge3(int arr[],int arr1[],int arr2[], int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        int L1[]=new int[n1];
        int R1[]=new int[n2];
        int L2[]=new int[n1];
        int R2[]=new int[n2];
        //long L3[]=new long[n1];
        //long R3[]=new long[n2];
        for (int i=0; i<n1; ++i)
        {
            L[i] = arr[l + i];
            L1[i]=arr1[l+i];
            L2[i]=arr2[l+i];
            //L3[i]=arr3[l+i];
        }
        for (int j=0; j<n2; ++j)
        {
            R[j] = arr[m + 1+ j];
            R1[j]=arr1[m+1+j];
            R2[j]=arr2[m+1+j];
            //R3[j]=arr3[m+1+j];
        }
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                arr1[k]=L1[i];
                arr2[k]=L2[i];
                //arr3[k]=L3[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                arr1[k]=R1[j];
                arr2[k]=R2[j];
                //arr3[k]=R3[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            arr1[k]=L1[i];
            arr2[k]=L2[i];
            //arr3[k]=L3[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            arr1[k]=R1[j];
            arr2[k]=R2[j];
            //arr3[k]=R3[j];
            j++;
            k++;
        }
    }
    void sort3(int arr[],int arr1[],int arr2[], int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort3(arr,arr1,arr2, l, m);
            sort3(arr ,arr1,arr2, m+1, r);
            merge3(arr,arr1,arr2,l, m, r);
        }
    }
    void merge2(int arr[],int arr1[],int l, int m, int r)
    {
        int n1 = m - l + 1;
        int n2 = r - m;
        int L[] = new int [n1];
        int R[] = new int [n2];
        int L1[]=new int[n1];
        int R1[]=new int[n2];
        for (int i=0; i<n1; ++i)
        {
            L[i] = arr[l + i];
            L1[i]=arr1[l+i];
        }
        for (int j=0; j<n2; ++j)
        {
            R[j] = arr[m + 1+ j];
            R1[j]=arr1[m+1+j];
        }
        int i = 0, j = 0;
        int k = l;
        while (i < n1 && j < n2)
        {
            if (L[i] <= R[j])
            {
                arr[k] = L[i];
                arr1[k]=L1[i];
                i++;
            }
            else
            {
                arr[k] = R[j];
                arr1[k]=R1[j];
                j++;
            }
            k++;
        }
        while (i < n1)
        {
            arr[k] = L[i];
            arr1[k]=L1[i];
            i++;
            k++;
        }
        while (j < n2)
        {
            arr[k] = R[j];
            arr1[k]=R1[j];
            j++;
            k++;
        }
    }
    void sort2(int arr[],int arr1[],int l, int r)
    {
        if (l < r)
        {
            int m = (l+r)/2;
            sort2(arr,arr1, l, m);
            sort2(arr ,arr1, m+1, r);
            merge2(arr,arr1,l, m, r);
        }
    }
    public static void main(String[] args) throws FileNotFoundException {
     try
     {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);
        templ ob=new templ();
        long a=in.nextLong();
        long b=in.nextLong();
        int m[]=new int[20];
        int o=0;
        int n[]=new int[20];
        int oo=0;
        long c=a;
        while(c!=0)
        {
            int d=(int)(c%10);
            m[o]=d;
            o++;
            c=c/10;
        }
        ob.sort1(m,0,o-1);
        long d=b;
        while(d!=0)
        {
            int k=(int)(d%10);
            n[oo]=k;
            oo++;
            d=d/10;
        }
        for(int i=0;i<oo/2;i++)
        {
            int temp=n[i];
            n[i]=n[oo-1-i];
            n[oo-1-i]=temp;
        }
        if(oo>o)
        {
            for(int i=o-1;i>=0;i--)
                out.print(m[i]);
        }
        else
        {
        int dest[]=new int[20];
        int p=0;
        int vis[]=new int[o];
        for(int i=0;i<oo;i++)
        {
            int x=n[i];
            int prev=-1;
            int j1=-1;
            for(int j=0;j<o;j++)
            {
                int y=m[j];
                if(y>x)
                    break;
                else if(y<=x&&vis[j]==0)
                {
                    prev=y;
                    j1=j;
                }
            }
            if(prev!=-1)
            {
                dest[p]=prev;
                p++;
                vis[j1]=1;
                if(prev<x)
                {
                    for(int l=o-1;l>=0;l--)
                    {
                        if(vis[l]==0)
                        {
                            dest[p]=m[l];
                            p++;
                            vis[l]=1;
                        }
                    }
                    break;
                }
            }
            else
            {
                int max=-1;
                int jj=-1;
                for(int l=p-1;l>=0;l--)
                {
                    //for(int ll=0;ll<o;ll++)
                        //out.println(m[ll]+" "+vis[ll]);
                    for(int ll=0;ll<o;ll++)
                    {
                        if(vis[ll]==0&&m[ll]<dest[l]&&m[ll]>max)
                        {
                            max=m[ll];
                            jj=ll;
                        }
                    }
                    if(max!=-1)
                    {
                        vis[jj]=1;
                        int kk=dest[l];
                        dest[l]=max;
                        for(int ll=0;ll<o;ll++)
                        {
                            if(vis[ll]==1&&m[ll]==kk)
                            {
                                vis[ll]=0;
                                break;
                            }
                        }
                        break;
                    }
                    else
                    {
                        int kk=dest[l];
                        //out.println(kk);
                        p--;
                        for(int ll=0;ll<o;ll++)
                        {
                            if(vis[ll]==1&&m[ll]==kk)
                            {
                                vis[ll]=0;
                                break;
                            }
                        }
                    }
                }
                for(int l=o-1;l>=0;l--)
                {
                    if(vis[l]==0)
                    {
                        dest[p]=m[l];
                        p++;
                        vis[l]=1;
                    }
                }
                break;
            }
        }
        for(int i=0;i<p;i++)
            out.print(dest[i]);
        }
        out.close();
    }
        catch(Exception e){
        return;
    }
    }
    static class InputReader {

        private final InputStream stream;
        private final byte[] buf = new byte[8192];
        private int curChar, snumChars;

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

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

        public int nextInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                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 {
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

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

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

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

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

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

    }
}


import java.util.*;
import java.util.LinkedList;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.math.BigInteger;

public class tmp {
    public static void main(String [] args) throws Exception{
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(in.readLine());
        StringBuilder sb = new StringBuilder();

        int n = Integer.parseInt(st.nextToken());
        int a = Integer.parseInt(st.nextToken());
        int [] arr = new int[n+1];
        st = new StringTokenizer(in.readLine());

        for(int i=1; i<=n; i++){
            arr[i] = Integer.parseInt(st.nextToken());
        }
        int out = 0;
        if(a <= n/2){
            for(int i=1; i<a; i++){
                if(arr[i] + arr[2*a-i] == 2)
                    out+=2;
            }
            out += arr[a];
            for(int i=2*a; i<=n; i++){
                out += arr[i];
            }
        }else{
            for(int i=n; i>a; i--){
                if(arr[i] + arr[2*a-i] == 2)
                    out+=2;
            }
            out += arr[a];
            for(int i=1; i<2*a-n; i++){
                out += arr[i];
            }
        }

        System.out.println(out);

    }

}

class P implements Comparable<P>{
    int val, idx;
    public P(int val, int idx){
        this.val = val;
        this.idx = idx;
    }

    public int compareTo(P other){
        if (this.val != other.val) return -this.val + other.val;
        return this.idx - other.idx;
    }
}


import java.util.*;
import java.io.*;
public final class Main {
    static long pow(long n, long x, long mod) {
        long res = 1;
        while(x>0) {
            if(x%2==1)
                res = (res*n)%mod;
            x>>=1;
            n = (n*n)%mod;
        }
        return res;
    }
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        long n = sc.nextLong();
        long m = sc.nextLong();
        long ans = pow(3, n, m) - 1;
        if(ans<0)
            ans+=m;
        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 qq
 */
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);
        TaskC solver = new TaskC();
        solver.solve(1, in, out);
        out.close();
    }

    static class TaskC {
        public int check(int n, Integer x, int len) {
            int ans = 0;
            while (n > 0 && x > 0) {
                if (n % 10 == x % 10) {
                    ans++;
                    x /= 10;
                }

                n /= 10;
            }

            if (x == 0) return len - ans;
            else return 100;

        }

        public void solve(int testNumber, Scanner in, PrintWriter out) {
            Integer n = in.nextInt();
            int len = n.toString().length();
            int ans = 100;
            for (int i = 1; i * i <= n; i++) {
                ans = Math.min(check(n, i * i, len), ans);
            }

            if (ans != 100) out.println(ans);
            else out.println(-1);
        }

    }
}


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

public class Main {
    public static void main(String[] args) throws IOException {
        //Code starts

        FastReader ip = new FastReader();
        OutputStream output = System.out;
        PrintWriter out = new PrintWriter(output);
        String s=ip.nextLine();
        int arr[]=new int[6];
        int sum1=0;
        int sum2=0;
        for(int i=0;i<6;i++){
            arr[i]=s.charAt(i)-'0';
            if(i<3){
                sum1+=arr[i];
            }else{
                sum2+=arr[i];
            }
        }

        if(sum1==sum2){
            out.println(0);
        }else {
            int m1 = 0, m2 = 0, m3 = 0, M1 = 0, M2 = 0, M3 = 0;
            int diff=0;
            if (sum2 > sum1) {
                diff=sum2-sum1;
                m1 = Math.min(arr[0], Math.min(arr[1], arr[2]));
                m3 = Math.max(arr[0], Math.max(arr[1], arr[2]));
                m2 = (sum1 - m1) - m3;
                M1 = Math.min(arr[3], Math.min(arr[4], arr[5]));
                M3 = Math.max(arr[3], Math.max(arr[4], arr[5]));
                M2 = (sum2 - M1) - M3;
            } else {
                diff=sum1-sum2;
                m1 = Math.min(arr[3], Math.min(arr[4], arr[5]));
                m3 = Math.max(arr[3], Math.max(arr[4], arr[5]));
                m2 = (sum2 - m1) - m3;
                M1 = Math.min(arr[0], Math.min(arr[1], arr[2]));
                M3 = Math.max(arr[0], Math.max(arr[1], arr[2]));
                M2 = (sum1 - M1) - M3;
            }
            int c = 0;
            if (M3 > (9 - m1)) {
                if (diff > M3) {
                    diff -= M3;
                    c++;
                    if (M2 > (9 - m1)) {
                        if (diff > M2) {
                            c++;
                        }
                    } else {
                        if (diff > (9 - m1)) {
                            c++;
                        }
                    }
                }
            } else {
                if (diff > (9 - m1)) {
                    diff -= (9 - m1);
                    c++;
                    if (M3 > (9 - m2)) {
                        if (diff > M3) {
                            c++;
                        }
                    } else {
                        if (diff > (9 - m2)) {
                            c++;
                        }
                    }
                }

            }

         out.println(c+1);
        }
        out.close();
        //Code ends
    }

    static class FastReader {
        BufferedReader br;
        StringTokenizer st;
        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.io.*;
import java.util.*;
import static java.lang.Math.*;

public class santaandcandies2
{
	static int[] arr;
	static int inser_p;
	static int n;
	public static void main(String[] args)throws IOException {
		InputReader ir=new InputReader(System.in);
		OutputWriter ow=new OutputWriter(System.out);
		precompute();
		inser_p=((inser_p=Arrays.binarySearch(arr,n=ir.readInt()))<0?(abs(inser_p)-2):inser_p);
		ow.printLine(inser_p);
		int i=1;
		for(;i<inser_p;++i)
			ow.print(i+" ");
		ow.printLine(i+(n-arr[inser_p]));
		ow.flush();
		ow.close();
	}
	static void precompute()
	{
		int mul=0,n=1;
		arr=new int[45];
       while((mul=(n*(n+1)/2))<=1000)
       	 arr[n++]=mul;
	}
	  static  class InputReader {
    	private InputStream stream;
    	private byte[] buf = new byte[1024];
    	private int curChar;
    	private int numChars;
    	private SpaceCharFilter filter;

    	public InputReader(InputStream stream) {
    		this.stream = stream;
    		}
    	public int read() {
    		if (numChars == -1)
    		    throw new InputMismatchException();
    		if (curChar >= numChars) {
    			curChar = 0;
    		try {
    				numChars = stream.read(buf);
    			} catch (IOException e) {
    				throw new InputMismatchException();
    			}
    		if (numChars <= 0)
    			return -1;
    			}
    			return buf[curChar++];
    		}
    	public int readInt() {
    	    int c = read();
    		while (isSpaceChar(c))
    				c = read();
    		int sgn = 1;
    		if (c == '-') {
    			sgn = -1;
    			c = read();
    	}
    			int res = 0;
    			do {
    				if (c < '0' || c > '9')
    					throw new InputMismatchException();
    				res *= 10;
    				res += c - '0';
    				c = read();
    			} while (!isSpaceChar(c));
    			return res * sgn;
    		}
    		public String readString() {
    			int c = read();
    			while (isSpaceChar(c))
    				c = read();
    			StringBuilder res = new StringBuilder();
    			do {
    				res.appendCodePoint(c);
    				c = read();
    			} while (!isSpaceChar(c));
    			return res.toString();
    		}
    		public boolean isSpaceChar(int c) {
    			if (filter != null)
    				return filter.isSpaceChar(c);
    			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
    		}
    		public String next() {
    			return readString();
    		}
    		public interface SpaceCharFilter {
    			public boolean isSpaceChar(int ch);
    		}
    	}
   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(Object...objects) {
    			for (int i = 0; i < objects.length; i++) {
    				if (i != 0)
    					writer.print(' ');
    				writer.print(objects[i]);
    			}
    		}
    		public void printLine(Object...objects) {
    			print(objects);
    			writer.println();
    		}
    		public void close() {
    			writer.close();
    		}
    		public void flush() {
    			writer.flush();
    		}
    		}
}


import java.io.File;
import java.io.FileNotFoundException;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Q3E {

public static int gcd (int a,int b)
{
    if (b==0)
        return a;
    return gcd(b,a%b);
}
    public static void main(String[] args) throws FileNotFoundException
    {
        Scanner in = new Scanner(System.in);

        int n =  in.nextInt();

        int count = 0;

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

            int commondivisor = gcd(n-1, i);
            if (commondivisor == 1)
                count ++;
        }
        System.out.println(count);
    }
}


import java.util.Scanner;

/**
 * Created by PQZR3864 on 2018-10-04.
 */
public class PhoneNumber {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int count = 0;
        int n = input.nextInt();
        String digits = input.next();

        for(int i=0 ; i<digits.length() ;i++){
            if(digits.charAt(i) == '8') count++;
        }
        count = count > (n/11) ? (n/11) : count ;
        System.out.println(count);
        input.close();
    }
}


import java.math.BigInteger;
import java.util.*;

public class Main {
    static final int MOD = 1000000007;
    public static int count(int height , int width) {
        boolean[][] field = new boolean[height][width];
        int[][][][] res = new int[2][2][width][width];
        int totalRes = 0;
        for (int row = 0; row < height; ++row) {
            int[][][][] newRes = new int[2][2][width][width];
            for (int fD = 0; fD < 2; ++fD)
                for (int lD = 0; lD < 2; ++lD)
                    for (int fC = 0; fC < width; ++fC)
                        for (int lC = fC; lC < width; ++lC) {
                            int cur = res[fD][lD][fC][lC];
                            if (cur == 0) continue;
                            for (int newfD = fD; newfD < 2; ++newfD)
                                for (int newlD = lD; newlD < 2; ++newlD) {
                                    int newfC;
                                    if (newfD == 0)
                                        newfC = fC;
                                    else if (fD == 0)
                                        newfC = fC + 1;
                                    else
                                        newfC = fC;
                                    int newlC;
                                    if (newlD == 0)
                                        newlC = lC;
                                    else if (lD == 0)
                                        newlC = lC - 1;
                                    else
                                        newlC = lC;
                                    if (newfC > newlC)
                                        continue;
                                    int[] arr = newRes[newfD][newlD][newfC];
                                    arr[newlC] += cur;
                                    if (arr[newlC] >= MOD) arr[newlC] -= MOD;
                                }
                        }

            for (int fD = 0; fD < 2; ++fD)
                for (int lD = 0; lD < 2; ++lD) {
                    int[][] arr = newRes[fD][lD];
                    if (fD == 0 && lD == 1) {
                        {
                            int lastDelta = lD == 0 ? 1 : -1;
                            int lastMin = lastDelta == 1 ? 0 : width - 1;
                            int lastMax = lastDelta == 1 ? width : -1;
                            for (int lC = lastMin; lC != lastMax; lC += lastDelta) {
                                for (int fC = 0; fC <= lC; ++fC) {
                                    int cur = arr[fC][lC];
                                    if (cur == 0) continue;
                                    int newlC = lC + lastDelta;
                                    if (newlC >= fC && newlC < width) {
                                        arr[fC][newlC] += cur;
                                        if (arr[fC][newlC] >= MOD) arr[fC][newlC] -= MOD;
                                    }
                                }
                            }
                        }
                        {
                            int firstDelta = fD == 0 ? -1 : 1;
                            int firstMin = firstDelta == 1 ? 0 : width - 1;
                            int firstMax = firstDelta == 1 ? width : -1;
                            for (int fC = firstMin; fC != firstMax; fC += firstDelta) {
                                for (int lC = fC; lC < width; ++lC) {
                                    int cur = arr[fC][lC];
                                    if (cur == 0) continue;
                                    int newfC = fC + firstDelta;
                                    if (newfC >= 0 && newfC <= lC) {
                                        arr[newfC][lC] += cur;
                                        if (arr[newfC][lC] >= MOD) arr[newfC][lC] -= MOD;
                                    }
                                }
                            }
                        }
                    } else {
                        {
                            int firstDelta = fD == 0 ? -1 : 1;
                            int firstMin = firstDelta == 1 ? 0 : width - 1;
                            int firstMax = firstDelta == 1 ? width : -1;
                            for (int fC = firstMin; fC != firstMax; fC += firstDelta) {
                                for (int lC = fC; lC < width; ++lC) {
                                    int cur = arr[fC][lC];
                                    if (cur == 0) continue;
                                    int newfC = fC + firstDelta;
                                    if (newfC >= 0 && newfC <= lC) {
                                        arr[newfC][lC] += cur;
                                        if (arr[newfC][lC] >= MOD) arr[newfC][lC] -= MOD;
                                    }
                                }
                            }
                        }
                        {
                            int lastDelta = lD == 0 ? 1 : -1;
                            int lastMin = lastDelta == 1 ? 0 : width - 1;
                            int lastMax = lastDelta == 1 ? width : -1;
                            for (int lC = lastMin; lC != lastMax; lC += lastDelta) {
                                for (int fC = 0; fC <= lC; ++fC) {
                                    int cur = arr[fC][lC];
                                    if (cur == 0) continue;
                                    int newlC = lC + lastDelta;
                                    if (newlC >= fC && newlC < width) {
                                        arr[fC][newlC] += cur;
                                        if (arr[fC][newlC] >= MOD) arr[fC][newlC] -= MOD;
                                    }
                                }
                            }
                        }
                    }
                }

            for (int fC = 0; fC < width; ++fC) {
                boolean ok = true;
                for (int lC = fC; lC < width; ++lC) {
                    if (field[row][lC])
                        ok = false;
                    if (!ok) {
                        for (int fD = 0; fD < 2; ++fD)
                            for (int lD = 0; lD < 2; ++lD)
                                newRes[fD][lD][fC][lC] = 0;
                   } else {
                        newRes[0][0][fC][lC] += 1;
                        if (newRes[0][0][fC][lC] >= MOD) newRes[0][0][fC][lC] -= MOD;
                    }
                }
            }

            res = newRes;
            for (int fD = 0; fD < 2; ++fD)
                for (int lD = 0; lD < 2; ++lD)
                    for (int fC = 0; fC < width; ++fC)
                        for (int lC = fC; lC < width; ++lC) {
                            totalRes += res[fD][lD][fC][lC];
                            if (totalRes >= MOD) totalRes -= MOD;
                        }
        }
        return totalRes;
    }
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        int n = cin.nextInt() , m = cin.nextInt();
        System.out.println(count(n, m));
    }
}


    import  java.util.Scanner;
    import java.util.Vector;

    public class A_VanyaAndCubes {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            int cubes = input.nextInt();
            /*
            int [] arr = new int [cubes+1];

            arr[0] = 1 ;

            for(int i = 1 ; i < cubes ; i++)
                for(int j = 1 ; j <= i+1 ;j++)
                    arr[i] += j;

            int total = 0,count = 0 ;
            while (cubes > total)
                total += arr[count++];
            System.out.print(total == count? count :count-1);
            */
            int x = 0 ;
            int total = 0 ;
            int count = 0 ;
            for(int i = 1 ; i <= cubes ; i++){
                x += i;
                total += x;
                if(total > cubes)
                    break;
                count++;
            }
            System.out.print(count);
        }
    }


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

public class Main {

	static int mod = (int) 1e9 + 7;
	static int MAX = (int) 1e7;

	public static void main(String[] args) {
		FasterScanner s = new FasterScanner();
		PrintWriter out = new PrintWriter(System.out);
		int t = 1;
		loop: while (t-- > 0) {
			int n = s.nextInt();
			int[] a = s.nextIntArray(n);
			int[] c = new int[1001];
			for(int i=0;i<n;i++){
				c[a[i]]++;
			}
			for(int i=0;i<1001-2;i++){
				if(c[i]>0 && c[i+1]>0 && c[i+2]>0){
					out.println("YES");
					continue loop;
				}
			}
			out.println("NO");
		}
		out.close();
	}

	static void dfs1(List<Integer>[] graph, boolean[] used, List<Integer> order, int u) {
		used[u] = true;
		for (int v : graph[u])
			if (!used[v])
				dfs1(graph, used, order, v);
		order.add(u);
	}

	static void dfs2(List<Integer>[] reverseGraph, int[] comp, int u, int color) {
		comp[u] = color;
		for (int v : reverseGraph[u])
			if (comp[v] == -1)
				dfs2(reverseGraph, comp, v, color);
	}

	public static boolean[] solve2Sat(List<Integer>[] graph) {
		int n = graph.length;
		boolean[] used = new boolean[n];
		List<Integer> order = new ArrayList<>();
		for (int i = 0; i < n; ++i)
			if (!used[i])
				dfs1(graph, used, order, i);

		List<Integer>[] reverseGraph = new List[n];
		for (int i = 0; i < n; i++)
			reverseGraph[i] = new ArrayList<>();
		for (int i = 0; i < n; i++)
			for (int j : graph[i])
				reverseGraph[j].add(i);

		int[] comp = new int[n];
		Arrays.fill(comp, -1);
		for (int i = 0, color = 0; i < n; ++i) {
			int u = order.get(n - i - 1);
			if (comp[u] == -1)
				dfs2(reverseGraph, comp, u, color++);
		}

		for (int i = 0; i < n; ++i)
			if (comp[i] == comp[i ^ 1])
				return null;

		boolean[] res = new boolean[n / 2];
		for (int i = 0; i < n; i += 2)
			res[i / 2] = comp[i] > comp[i ^ 1];
		return res;
	}

	public static int[] createSets(int size) {
		int[] p = new int[size];
		for (int i = 0; i < size; i++)
			p[i] = i;
		return p;
	}

	public static int root(int[] p, int x) {
		return x == p[x] ? x : (p[x] = root(p, p[x]));
	}

	public static void unite(int[] p, int a, int b) {
		a = root(p, a);
		b = root(p, b);
		if (a != b)
			p[a] = b;
	}

	public static boolean nextPermutation(int[] p) {
		for (int a = p.length - 2; a >= 0; --a)
			if (p[a] < p[a + 1])
				for (int b = p.length - 1;; --b)
					if (p[b] > p[a]) {
						int t = p[a];
						p[a] = p[b];
						p[b] = t;
						for (++a, b = p.length - 1; a < b; ++a, --b) {
							t = p[a];
							p[a] = p[b];
							p[b] = t;
						}
						return true;
					}
		return false;
	}

	public static boolean isPrime(long n) {
		if (n <= 1)
			return false;
		for (long i = 2; i * i <= n; i++)
			if (n % i == 0)
				return false;
		return true;
	}

	static class SegTree {
		int[] val;
		int n;

		SegTree(int n) {
			val = new int[2 * n];
			for (int i = 0; i < n; i++)
				val[n + i] = i + 1;
			for (int i = n - 1; i >= 0; i--)
				val[i] = Math.max(val[2 * i], val[2 * i + 1]);
			this.n = n;
		}

		void upd(int p) {
			p += n;
			val[p] = -1;
			for (; (p >>= 1) != 0;) {
				val[p] = Math.max(val[2 * p], val[2 * p + 1]);
			}
		}

		int query(int l, int r) { // sum on interval [l, r)
			// System.out.println(l+" "+r);
			int res = -1;
			for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
				if ((l & 1) != 0)
					res = Math.max(res, val[l++]);
				if ((r & 1) != 0)
					res = Math.max(res, val[--r]);
			}
			return res;
		}
	}

	static boolean check(int i, int j, int n) {
		if (i >= 0 && j >= 0 && i < n && j < n)
			return true;
		return false;
	}

	public static int[] generateDivisorTable(int n) {
		int[] divisor = new int[n + 1];
		for (int i = 1; i <= n; i++)
			divisor[i] = i;
		for (int i = 2; i * i <= n; i++)
			if (divisor[i] == i)
				for (int j = i * i; j <= n; j += i)
					divisor[j] = i;
		return divisor;
	}

	public static int[][] matrixMul(int[][] a, int[][] b) {
		int n = a.length;
		int m = a[0].length;
		int k = b[0].length;
		int[][] res = new int[n][k];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < k; j++) {
				for (int p = 0; p < m; p++) {
					res[i][j] = (res[i][j] + (int) ((a[i][p] * 1l * b[p][j]) % mod)) % mod;
				}
			}
		}
		return res;
	}

	public static int[][] matrixPow(int[][] a, int p) {
		if (p == 0) {
			return matrixUnit(a.length);
		} else if (p % 2 == 0) {
			return matrixPow(matrixMul(a, a), p / 2);
		} else {
			return matrixMul(a, matrixPow(a, p - 1));
		}
	}

	public static int[][] matrixUnit(int n) {
		int[][] res = new int[n][n];
		for (int i = 0; i < n; ++i) {
			res[i][i] = 1;
		}
		return res;
	}

	static int[] modInv(int n, int mod) {
		int[] ans = new int[n];
		for (int i = 1; i < n; i++)
			ans[i] = (int) pow(i, mod - 2, mod);
		return ans;
	}

	static int[] primes;

	public static long ncr(int n, int r) {
		if (n < r)
			return 0;
		if (n == r)
			return 1;
		if (r == 0)
			return 1;
		if (r < 0)
			return 0;
		long ans = 1;
		for (int i = 0; i < primes.length; i++) {
			long power = count(n, primes[i]) - count(n - r, primes[i]) - count(r, primes[i]);
			ans *= pow(primes[i], power, mod);
			ans %= mod;
		}
		return ans;
	}

	public static long pow(long x, long n, int mod) {
		long res = 1;
		for (long p = x; n > 0; n >>= 1, p = (p * p) % mod) {
			if ((n & 1) != 0) {
				res = (int) (res * p % mod);
			}
		}
		return res;
	}

	public static long count(int n, int p) {
		long count = 0;
		long z = p;
		while (z <= n) {
			count += n / z;
			z *= p;
		}
		return count;

	}

	public static int[] generatePrimes(int n) {
		boolean[] prime = new boolean[n + 1];
		Arrays.fill(prime, 2, n + 1, true);
		for (int i = 2; i * i <= n; i++)
			if (prime[i])
				for (int j = i * i; j <= n; j += i)
					prime[j] = false;
		int[] primes = new int[n + 1];
		int cnt = 0;
		for (int i = 0; i < prime.length; i++)
			if (prime[i])
				primes[cnt++] = i;

		return Arrays.copyOf(primes, cnt);
	}

	static class Pairxy implements Comparable<Pairxy> {
		int x;
		int y;

		Pairxy(int x, int y) {
			this.x = x;
			this.y = y;
		}

		public int compareTo(Pairxy o) {
			return Integer.compare(this.x, o.x);
		}

	}

	static class Pair implements Comparable<Pair> {
		long val;
		int ind;

		Pair(long v, int i) {
			val = v;
			ind = i;
		}

		public int compareTo(Pair o) {
			if (this.val != o.val)
				return Long.compare(this.val, o.val);
			return Integer.compare(this.ind, o.ind);
		}
	}

	public static long C(int n, int r) {
		long ans = 1;
		int x = 1;
		while (x <= r) {
			ans *= n;
			ans /= x;
			x++;
			n--;
		}
		return ans;
	}

	static class DisjointSetsRank {

		int[] p;
		int[] rank;

		public DisjointSetsRank(int size) {
			p = new int[size];
			for (int i = 0; i < size; i++)
				p[i] = i;
			rank = new int[size];
		}

		public int root(int x) {
			return x == p[x] ? x : (p[x] = root(p[x]));
		}

		public int sameSet(int x, int y) {
			if (root(x) == root(y))
				return 1;
			return 0;

		}

		public void unite(int a, int b) {
			a = root(a);
			b = root(b);
			if (a == b)
				return;

			if (rank[a] < rank[b]) {
				p[a] = b;
			} else {
				p[b] = a;
				if (rank[a] == rank[b])
					++rank[a];
			}
		}

	}

	static int gcd(long m, long n) {
		long x;
		long y;
		while (m % n != 0) {
			x = n;
			y = m % n;
			m = x;
			n = y;
		}
		return (int) n;
	}

	static class FasterScanner {
		private byte[] buf = new byte[1024];
		private int curChar;
		private int snumChars;

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

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

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

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

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

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

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

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

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


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

public class B {

	static int n, t[], g[], MOD = (int) 1e9 + 7;
	static int[][][] memo1, memo2[], memo3[];

	static int dp1(int idx, int remCnt, int remSum) {
		if (idx == n)
			return remSum == 0 && remCnt == 0 ? 1 : 0;
		if (remCnt < 0 || remSum < 0)
			return 0;
		if (memo1[idx][remCnt][remSum] != -1)
			return memo1[idx][remCnt][remSum];
		int ans = dp1(idx + 1, remCnt, remSum);
		if (g[idx] == 0) {
			ans += dp1(idx + 1, remCnt - 1, remSum - t[idx]);
			if (ans >= MOD)
				ans -= MOD;
		}
		return memo1[idx][remCnt][remSum] = ans;
	}

	static int dp2(int idx, int remCnt1, int remCnt2, int remSum) {

		if (idx == n)
			return remSum == 0 && remCnt1 == 0 && remCnt2 == 0 ? 1 : 0;
		if (remSum < 0 || remCnt1 < 0 || remCnt2 < 0)
			return 0;
		if (memo2[idx][remCnt1][remCnt2][remSum] != -1)
			return memo2[idx][remCnt1][remCnt2][remSum];
		int ans = dp2(idx + 1, remCnt1, remCnt2, remSum);

		if (g[idx] == 1)
			ans += dp2(idx + 1, remCnt1 - 1, remCnt2, remSum - t[idx]);
		else if (g[idx] == 2)
			ans += dp2(idx + 1, remCnt1, remCnt2 - 1, remSum - t[idx]);
		if(ans>=MOD)
			ans-=MOD;
		return memo2[idx][remCnt1][remCnt2][remSum] = ans;
	}

	private static int dp3(int cnt0, int cnt1, int cnt2, int last) {
		if (cnt0 < 0 || cnt1 < 0 || cnt2 < 0)
			return 0;
		if (cnt0 + cnt1 + cnt2 == 0)
			return 1;
		if (memo3[last][cnt0][cnt1][cnt2] != -1)
			return memo3[last][cnt0][cnt1][cnt2];
		long ans = 0;
		if (last != 0)
			ans += dp3(cnt0 - 1, cnt1, cnt2, 0);
		if (last != 1)
			ans += dp3(cnt0, cnt1 - 1, cnt2, 1);
		if (last != 2)
			ans += dp3(cnt0, cnt1, cnt2 - 1, 2);
		return memo3[last][cnt0][cnt1][cnt2] = (int) (ans % MOD);

	}

	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner();
		PrintWriter out = new PrintWriter(System.out);
		n = sc.nextInt();
		int[] fac = new int[n + 1];
		t = new int[n];
		g = new int[n];
		int[] cnt = new int[3];
		fac[0] = 1;
		for (int i = 1; i <= n; i++)
			fac[i] = (int) (i * 1L * fac[i - 1] % MOD);
		int T = sc.nextInt();
		for (int i = 0; i < n; i++) {
			t[i] = sc.nextInt();
			g[i] = sc.nextInt() - 1;
			cnt[g[i]]++;

		}
		memo1 = new int[n][cnt[0] + 1][T + 1];
		memo2 = new int[n][cnt[1] + 1][cnt[2] + 1][T + 1];
		memo3 = new int[4][cnt[0] + 1][cnt[1] + 1][cnt[2] + 1];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j <= cnt[0]; j++)
				Arrays.fill(memo1[i][j], -1);
			for (int j = 0; j <= cnt[1]; j++)
				for (int k = 0; k <= cnt[2]; k++)
					Arrays.fill(memo2[i][j][k], -1);

		}
		for (int i = 0; i < 4; i++)
			for (int j = 0; j <= cnt[0]; j++)
				for (int k = 0; k <= cnt[1]; k++)
					Arrays.fill(memo3[i][j][k], -1);
		int ans = 0;
		for (int cnt0 = 0; cnt0 <= cnt[0]; cnt0++)
			for (int sum0 = 0; sum0 <= T; sum0++)
				for (int cnt1 = 0; cnt1 <= cnt[1]; cnt1++)
					for (int cnt2 = 0; cnt2 <= cnt[2]; cnt2++) {
						long ways = dp1(0, cnt0, sum0) * 1L * dp2(0, cnt1, cnt2, T - sum0) % MOD;
						ways = ways * dp3(cnt0, cnt1, cnt2, 3) % MOD;
						ways *= fac[cnt0];
						ways %= MOD;
						ways *= fac[cnt1];
						ways %= MOD;
						ways *= fac[cnt2];
						ways %= MOD;
						ans += ways;
						if (ans >= MOD)
							ans -= MOD;
					}
		out.println(ans);
		out.close();

	}

	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

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

		Scanner(String fileName) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(fileName));
		}

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

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

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

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

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

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

	}

}


// https://codeforces.com/problemset/problem/1113/A
import java.util.*;import java.io.*;import java.math.*;
public class Main
{

	public static void process()throws IOException
    {
    	int n=ni(),v=ni();
    	int cost=0;

    	if(v>=n-1){
    		pn(n-1);
    		return;
    	}

    	cost+=v;
    	for(int i=2;i<=n-v;i++)
    		cost+=i;
    	pn(cost);
    }

   	static AnotherReader sc;

    public static void main(String[]args)throws IOException
    {
    	boolean oj = System.getProperty("ONLINE_JUDGE") != null;
    	if(oj)
        	sc=new AnotherReader();
        else
        	sc=new AnotherReader(100);
        int t=1;
        while(t-->0)
            process();
        System.out.flush();
        System.out.close();
    }

    static void pn(Object o){System.out.println(o);}
    static void p(Object o){System.out.print(o);}
    static void pni(Object o){System.out.println(o);System.out.flush();}
    static int ni()throws IOException{return sc.nextInt();}
    static long nl()throws IOException{return sc.nextLong();}
    static double nd()throws IOException{return sc.nextDouble();}
    static String nln()throws IOException{return sc.nextLine();}
    static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
    static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
    static boolean multipleTC=false;

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

    static class AnotherReader{BufferedReader br; StringTokenizer st;
    AnotherReader()throws FileNotFoundException{
    br=new BufferedReader(new InputStreamReader(System.in));}
    AnotherReader(int a)throws FileNotFoundException{
    br = new BufferedReader(new FileReader("input.txt"));}
    String next()throws IOException{
    while (st == null || !st.hasMoreElements()) {try{
    st = new StringTokenizer(br.readLine());}
    catch (IOException  e){ e.printStackTrace(); }}
    return st.nextToken(); } int nextInt() throws IOException{
    return Integer.parseInt(next());}
    long nextLong() throws IOException
    {return Long.parseLong(next());}
    double nextDouble()throws IOException { return Double.parseDouble(next()); }
    String nextLine() throws IOException{ String str = ""; try{
    str = br.readLine();} catch (IOException e){
    e.printStackTrace();} return str;}}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}


/*
ID: davidzh8
PROG: subset
LANG: JAVA
 */

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

public class villagemin {

    static long startTime = System.nanoTime();
    static ArrayList<Integer>[] adj;
    static int[] pos;
    public static void solve() throws Exception {
        boolean debug = (true);
        FastScanner sc = new FastScanner("villagemin.in", debug);
        FastWriter pw = new FastWriter("villagemin.out", debug);
        int N= sc.ni();
        adj= new ArrayList[N];
        pos= new int[N];
        for (int i = 0; i < N; i++) {
            pos[i]=i;
        }
        for(int i=0; i<N; i++){
            adj[i]= new ArrayList<>();
        }
        for (int i = 0; i < N-1; i++) {
            int a= sc.ni()-1;
            int b= sc.ni()-1;
            adj[a].add(b);
            adj[b].add(a);
        }
        dfs(0,-1);
        System.out.println(res);
        for (int i = 0; i < N; i++) {
            System.out.print(pos[i]+1+" ");
        }

        long endTime = System.nanoTime();
        //System.out.println("Execution Time: " + (endTime - startTime) / 1e9 + " s");
        pw.close();
    }
    static int res=0;
    static void dfs(int cur, int par){
        for(int next: adj[cur] ){
            if(next==par) continue;
            dfs(next, cur);
            if(pos[next]==next) {
                res += 2;
                int temp = pos[cur];
                pos[cur] = pos[next];
                pos[next] = temp;
            }
        }
        if(cur==0 && pos[cur]==0){
            int next= adj[0].get(0);
            res += 2;
            int temp = pos[cur];
            pos[cur] = pos[next];
            pos[next] = temp;
        }

    }

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

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

        public int compareTo(Pair obj) {
            if (obj.a == a) {
                return b - obj.b;
            } else return a - obj.a;
        }
    }

    static class djset {
        int N;
        int[] parent;

        // Creates a disjoint set of size n (0, 1, ..., n-1)
        public djset(int n) {
            parent = new int[n];
            N = n;
            for (int i = 0; i < n; i++)
                parent[i] = i;
        }

        public int find(int v) {

            // I am the club president!!! (root of the tree)
            if (parent[v] == v) return v;

            // Find my parent's root.
            int res = find(parent[v]);

            // Attach me directly to the root of my tree.
            parent[v] = res;
            return res;
        }

        public boolean union(int v1, int v2) {

            // Find respective roots.
            int rootv1 = find(v1);
            int rootv2 = find(v2);

            // No union done, v1, v2 already together.
            if (rootv1 == rootv2) return false;

            // Attach tree of v2 to tree of v1.
            parent[rootv2] = rootv1;
            return true;
        }

    }

    static class FastScanner {
        InputStream dis;
        byte[] buffer = new byte[1 << 17];
        int pointer = 0;

        public FastScanner(String fileName, boolean debug) throws Exception {
            if (debug) dis = System.in;
            else dis = new FileInputStream(fileName);
        }

        int ni() throws Exception {
            int ret = 0;

            byte b;
            do {
                b = nextByte();
            } while (b <= ' ');
            boolean negative = false;
            if (b == '-') {
                negative = true;
                b = nextByte();
            }
            while (b >= '0' && b <= '9') {
                ret = 10 * ret + b - '0';
                b = nextByte();
            }

            return (negative) ? -ret : ret;
        }

        long nl() throws Exception {
            long ret = 0;

            byte b;
            do {
                b = nextByte();
            } while (b <= ' ');
            boolean negative = false;
            if (b == '-') {
                negative = true;
                b = nextByte();
            }
            while (b >= '0' && b <= '9') {
                ret = 10 * ret + b - '0';
                b = nextByte();
            }

            return (negative) ? -ret : ret;
        }

        byte nextByte() throws Exception {
            if (pointer == buffer.length) {
                dis.read(buffer, 0, buffer.length);
                pointer = 0;
            }
            return buffer[pointer++];
        }

        String next() throws Exception {
            StringBuffer ret = new StringBuffer();

            byte b;
            do {
                b = nextByte();
            } while (b <= ' ');
            while (b > ' ') {
                ret.appendCodePoint(b);
                b = nextByte();
            }

            return ret.toString();
        }
    }

    static class FastWriter {
        public PrintWriter pw;

        public FastWriter(String name, boolean debug) throws IOException {
            if (debug) {
                pw = new PrintWriter(System.out);
            } else {
                pw = new PrintWriter(new BufferedWriter(new FileWriter(new File(name))));
            }
        }

        public void println(Object text) {
            pw.println(text);
        }

        public void print(Object text) {
            pw.print(text);
        }

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

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

    public static void main(String[] args) throws Exception {
        Thread thread = new Thread(null, () -> {
            try {
                solve();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "", 1 << 28);
        thread.start();
        thread.join();
    }

}


import java.util.Scanner;

public class KolyaAndTanya {
    public static void main(String[] args) {
        Scanner inputScanner = new Scanner(System.in);
        int n = inputScanner.nextInt();
        int bound = 1000000007;
        System.out.println((getBoundedPower(3, 3 * n, bound) - getBoundedPower(7, n, bound) + bound) % bound);
    }

    public static long getBoundedPower(int base, int power) {
        long res = 1;
        for (int i = 0; i < power; i++) {
            res = (res * base) % 1000000007;
        }
        return res ;
    }

    public static long getBoundedPower(int base, int power, int bound) {
        long ans = 1;
        long currentBase = base % bound;
        while (power > 0) {
            if (power % 2 == 1)
                ans = (ans * currentBase) % bound;
            currentBase = (currentBase * currentBase) % bound;
            power = power / 2;
        }
        return ans;
    }

}