109_E. Lucky Interval_36635
Petya loves lucky numbers. We all know that lucky numbers are the positive integers whose decimal representations contain only the lucky digits 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

One day Petya came across an interval of numbers [a, a + l - 1]. Let F(x) be the number of lucky digits of number x. Find the minimum b (a < b) such, that F(a) = F(b), F(a + 1) = F(b + 1), ..., F(a + l - 1) = F(b + l - 1).

Input

The single line contains two integers a and l (1 ≤ a, l ≤ 109) — the interval's first number and the interval's length correspondingly.

Output

On the single line print number b — the answer to the problem.

Examples

Input

7 4


Output

17


Input

4 7


Output

14

Note

Consider that [a, b] denotes an interval of integers; this interval includes the boundaries. That is, <image>
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

public class HappyNumbers implements Runnable {
    private void solve() throws IOException {
        /*for (long l = 1; l <= 1000; ++l) {
            System.out.println(l);
            for (long a = 1; a <= 1000; ++a) {
                if (doit(a, l) != doit2(a, l))
                    throw new RuntimeException(a + " " + l);
            }
        }*/
        long a = nextLong();
        long l = nextLong();
        System.out.println(doit(a, l));
    }

    /*private long doit2(long a, long l) {
        for (long b = a + 1;; ++b) {
            boolean ok = true;
            for (int i = 0; i < l; ++i)
                if (countLucky(a + i) != countLucky(b + i)) {
                    ok = false;
                    break;
                }
            if (ok)
                return b;
        }
    }*/

    private long doit(long a, long l) {
        if (l >= 10) {
            long first = a;
            long last = a + l - 1;
            return doit(first / 10, last / 10 - first / 10 + 1) * 10 + first % 10;
        } else {
            int[] cnts = new int[(int) l];
            for (int i = 0; i < l; ++i)
                cnts[i] = countLucky(a + i);
            return doitFrom(cnts, a + 1);
        }
    }

    private long doitFrom(int[] cnts, long min) {
        if (cnts.length == 1) {
            return doitOne(cnts[0], min);
        } else if (cnts.length == 2 && min <= 1) {
            return doitTwoSimple(cnts[0], cnts[1], min);
        }
        long res = Long.MAX_VALUE;
        for (int lastDigit = 0; lastDigit < 10; ++lastDigit) {
            int max = lastDigit + cnts.length - 1;
            int[] ncnts = new int[max / 10 + 1];
            Arrays.fill(ncnts, -1);
            boolean ok = true;
            for (int i = 0; i < cnts.length; ++i) {
                int cur = lastDigit + i;
                int need = cnts[i];
                if (cur % 10 == 4 || cur % 10 == 7)
                    --need;
                if (need < 0) {
                    ok = false;
                    break;
                }
                if (ncnts[cur / 10] < 0 || ncnts[cur / 10] == need) {
                    ncnts[cur / 10] = need;
                } else {
                    ok = false;
                    break;
                }
            }
            if (!ok) continue;
            long newMin = min / 10 - 3;
            while (newMin * 10 + lastDigit < min)
                ++newMin;
            long tmp = doitFrom(ncnts, newMin) * 10 + lastDigit;
            res = Math.min(res, tmp);
        }
        return res;
    }

    private long doitTwoSimple(int cntFirst, int cntSecond, long min) {
        if (cntFirst == 0 && cntSecond == 0)
            return min;
        if (cntFirst == cntSecond) {
            long res = 0;
            for (int i = 0; i < cntFirst; ++i)
                res = res * 10 + 4;
            return res * 10;
        } else if (cntFirst == cntSecond + 1) {
            long res = 0;
            for (int i = 0; i < cntFirst; ++i)
                res = res * 10 + 4;
            return res;
        } else if (cntFirst + 1 == cntSecond) {
            long res = 0;
            for (int i = 0; i < cntSecond; ++i)
                res = res * 10 + 4;
            return res - 1;
        } else {
            return Long.MAX_VALUE;
        }
    }

    private long doitOne(int cnt, long min) {
        long p10 = 1;
        int pow = 0;
        while (p10 <= min) {
            p10 *= 10;
            ++pow;
        }
        p10 *= 100;
        pow += 2;
        while (pow < cnt + 2) {
            p10 *= 10;
            ++pow;
        }
        long res = 0;
        while (true) {
            for (int curDigit = 0; curDigit < 10; ++curDigit) {
                int curCnt = 0;
                if (curDigit == 4 || curDigit == 7) curCnt = 1;
                if (possible(res, p10, pow, cnt - curCnt, min)) {
                    cnt -= curCnt;
                    break;
                }
                res += p10;
                if (curDigit == 9)
                    throw new RuntimeException();
            }
            if (p10 == 1) break;
            p10 /= 10;
            --pow;
        }
        return res;
    }

    private boolean possible(long res, long p10, int pow, int needLucky, long min) {
        if (needLucky > pow || needLucky < 0)
            return false;
        long cur = 0;
        for (int i = 0; i < pow - needLucky; ++i)
            cur = cur * 10 + 9;
        for (int i = 0; i < needLucky; ++i)
            cur = cur * 10 + 7;
        return res + cur >= min;
    }

    private int countLucky(long x) {
        int res = 0;
        while (x > 0) {
            long dig = x % 10;
            x /= 10;
            if (dig == 4 || dig == 7) ++res;
        }
        return res;
    }

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

    BufferedReader reader;
    StringTokenizer tokenizer;
    PrintWriter writer;

    public void run() {
        try {
            reader = new BufferedReader(new InputStreamReader(System.in));
            tokenizer = null;
            writer = new PrintWriter(System.out);
            solve();
            reader.close();
            writer.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.exit(1);
        }
    }

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

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

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

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

7 4
 4 7
 1 1000000000
 1000000000 47
 699999999 5
 500000000 500000000
 100 1
 85474999 1000000000
 46996 2
 4699999 1000000000
 9999 107545
 700000000 1000000000
 73999999 78
 399999999 1
 96555 40104
 4769999 2
 14 1000000000
 45749895 8445
 9548 748
 777777771 19
 469 1
 96 39
 39999997 4
 548454 845445
 699999999 2
 474474 7475
 699999 1
 39999999 1450
 46999999 185
 39997840 19
 95779999 1000000000
 1 1
 47497 1
 1000000000 1000000000
 744444430 10
 95 3
 9 1000000000
 774447447 1
 974999 4045
 39970 20
 699999997 4
 6777 1000
 399999997 25
 369999995 5
 474447474 1000
 474799099 1000000000
 17 17
 399999999 1000000000
 45999999 1000000000
 258963174 777777774
 444444444 1000000000
 747477777 1000000000
 7399999 1
 699997 1
 39997840 198
 399970 12
 47699984 54740
 399970 24
 499999977 395
 777777776 2
 22 484
 45999999 1000
 399999997 18
 999999999 999999999
 47000 1000
 48769999 1
 47447470 48545
 47 74
 45 18
 76954099 1000000
 6777 10000
 477 7
 39977 2
 85 98
 777777776 18
 73999 1
 397 1
 7776920 190
 777777770 19
 48769999 1000000000
 474447474 10000
 7499999 195
 476990 190
 540395 10
 10000 10000
 474799999 1024
 95458154 6845740
 45999999 200
 75 1000000000
 1000000000 1
 104 1
 777777771 18
 3945845 174
 48544954 199
 854799 1000000000
 747444777 1000000000
 399999999 145
 297 6
 965411551 55545859
 999999999 1
 4699999 1
 1 600
 39997820 190
 47 9454
 444444443 2
 777777777 100000000
 474447474 747774
 699999999 18
 47497 457
 37477 10000
 471 1000000000
 39970 4
 658458950 1000000000
 9654 2540
 7474447 1000000000
 6777 100000
 10 10
 777777777 150
 100 100
 444444444 10000
 370 10
 73990000 10000
 47 2
 46999 100000
 8748544 48248414
 458 10000
 5456954 448559900
 777 10
 46000 10000
 474799999 1
 154215478 999547859
 7 1000000000
 39997840 190
 777777777 7
 777777777 3
 97 1
 1 8474585
 777777777 20
 4 1000000000
 954 71
 1 74548744
 45749895 18445
 144 197
 399999999 18
 399999997 15
 3997 3
 47497 900
 779999 1000000000
 739999999 69
 3699949 60
 499999977 99
 777777777 1
 399999999 20
 48769999 100
 747477 1000000000
 1 1000
 399999997 4
 469999370 10
 459544 48587874
 399999997 2
 777777777 18
 399999997 7
 1 1010000000
 1000000000 90
 699999999 9
 500000000 909311080
 000 1
 85474999 1100000000
 1949541 1000000000
 9999 182309
 181016732 1000000000
 31704616 78
 723826216 1
 111113 40104
 8183466 2
 17 1000000000
 78430600 8445
 9548 532
 777777771 32
 469 2
 124 39
 548454 55309
 292952806 2
 39999999 2734
 73940930 185
 39997840 38
 95779999 1000000010
 1000000010 1000000000
 791136708 10
 147 3
 3 1000000000
 148538323 1
 1163139 4045
 67800 20
 1029962631 4
 3757 1000
 455578275 25
 474447474 1001
 474799099 1100000000
 17 7
 579560762 1000000000
 60331519 1000000000
 258963174 434260043
 444444444 1000000100
 398981292 1000000000
 7399999 2
 1329026 1
 43988294 198
 124747 12
 47699984 97483
 399970 23
 499999977 554
 59043571 2
 22 209
 399999997 9
 48769999 2
 47 134
 1 18
 4388 10000
 678 7
 21002 2
 85 102
 469668086 18
 83162 1
 298 1
 1246338 190
 23068757 1000000000
 474447474 10100
 7499999 346
 132046 190
 540395 1
 10000 11000
 80394224 1024
 19162542 200
 125 1000000000
 1010000000 1
 104 2
 6691449 174
 48544954 9
 854799 1010000000
 770101927 145
 297 2
 6979407 1

17
 14
 1000000001
 1000000100
 800000039
 1200000000
 101
 1085474999
 46999
 1004699999
 109999
 1700000000
 75000399
 500000000
 186555
 4780003
 1000000014
 45779895
 10548
 1444444441
 480
 186
 50000004
 1548454
 800000003
 744474
 800000
 50039999
 48003999
 39997870
 1095779999
 2
 47544
 2000000000
 744444450
 102
 1000000009
 774447474
 1044999
 49990
 800000004
 13777
 500000397
 380000002
 474474474
 1474799099
 87
 1399999999
 1045999999
 1258963174
 1444444444
 1747477777
 7500000
 700000
 49999840
 499990
 74399984
 499990
 500000777
 1444444443
 1022
 46000999
 500000397
 1999999999
 74000
 48780000
 47747470
 147
 75
 143954099
 16777
 747
 40004
 185
 1444444446
 75000
 400
 14443920
 1444444440
 1048769999
 474477474
 7500499
 743990
 540535
 20000
 474804999
 105458154
 46000099
 1000000075
 1000000001
 107
 1444444441
 3945945
 48545454
 1000854799
 1747444777
 500003999
 307
 1065411551
 1000000000
 4800000
 1001
 49999820
 10047
 444444446
 1477777777
 477447474
 800000399
 74497
 67477
 1000000471
 40000
 1658458950
 10654
 1007474447
 106777
 20
 1444444477
 200
 444474444
 400
 75000000
 74
 146999
 108748544
 10458
 1005456954
 1447
 76000
 474800004
 1154215478
 1000000007
 49999840
 1444444447
 1444444444
 104
 10000001
 1444444447
 1000000004
 1054
 100000001
 45779895
 844
 500000399
 500000397
 4999
 74497
 1000779999
 750000399
 3800349
 500000477
 1444444444
 500000399
 48780399
 1000747477
 1001
 500000004
 469999400
 100459544
 499999999 1444444447 500000037 1000000001 1000000100 800000039 1500000000 1 1085474999 1001949541 109999 1181016732 31704816 723826218 211113 8183469 1000000017 78450600 10548 1444444471 483 154 848454 292952813 50039999 73941030 39997940 1095779999 2000000010 791136718 174 1000000003 148538325 1166139 67810 1029962651 6757 455578345 474474474 1474799099 27 1579560762 1060331519 558963174 1444444444 1398981292 7500003 1329028 43988594 124777 74399984 499990 500004977 59043572 122 500000037 48780003 147 11 14388 798 21005 185 469668096 83163 299 1246638 1023068757 474477474 7500799 132146 540396 20000 80397224 19163242 1000000125 1010000001 107 6691749 48544984 1000854799 770102027 304 6979414