Robbers, who attacked the Gerda's cab, are very successful in covering from the kingdom police. To make the goal of catching them even harder, they use their own watches.

First, as they know that kingdom police is bad at math, robbers use the positional numeral system with base 7. Second, they divide one day in n hours, and each hour in m minutes. Personal watches of each robber are divided in two parts: first of them has the smallest possible number of places that is necessary to display any integer from 0 to n - 1, while the second has the smallest possible number of places that is necessary to display any integer from 0 to m - 1. Finally, if some value of hours or minutes can be displayed using less number of places in base 7 than this watches have, the required number of zeroes is added at the beginning of notation.

Note that to display number 0 section of the watches is required to have at least one place.

Little robber wants to know the number of moments of time (particular values of hours and minutes), such that all digits displayed on the watches are distinct. Help her calculate this number.

Input

The first line of the input contains two integers, given in the decimal notation, n and m (1 ≤ n, m ≤ 109) — the number of hours in one day and the number of minutes in one hour, respectively.

Output

Print one integer in decimal notation — the number of different pairs of hour and minute, such that all digits displayed on the watches are distinct.

Examples

Input

2 3


Output

4


Input

8 2


Output

5

Note

In the first sample, possible pairs are: (0: 1), (0: 2), (1: 0), (1: 2).

In the second sample, possible pairs are: (02: 1), (03: 1), (04: 1), (05: 1), (06: 1).
//package baobab;

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

public class C {

    public static void main(String[] args) {
        CIO io = new CIO();
        try {
            Csolver solver = new Csolver(io);
            solver.solve();
        } finally {
            io.close();
        }
    }

}

class Csolver {
    CIO io;

    public Csolver(CIO io) {
        this.io = io;
    }

    static int n;
    static int m;
    static int maxHoursLength;
    static int maxMinutesLength;
    static long lastHourToDisplay;
    static long lastMinuteToDisplay;

    static long count;

    public void solve() {
        count = 0;
        n = io.nextInt();
        m = io.nextInt();
        int largestDigit = Math.min(6, Math.max(n, m) - 1);
        boolean[] av = new boolean[largestDigit+1];
        for (int i=0; i<=largestDigit; i++) {
            av[i] = true;
        }

        lastHourToDisplay = Long.parseLong(Long.toString(n-1, 7));
        lastMinuteToDisplay = Long.parseLong(Long.toString(m-1, 7));
        maxHoursLength = Long.toString(n-1, 7).length();
        maxMinutesLength = Long.toString(m-1, 7).length();
        int totalLength = maxHoursLength + maxMinutesLength;
        if (totalLength > 7) {
            System.out.println(0);
            return;
        }
        boolean[] picked = new boolean[largestDigit+1];
        pick(av, 0, picked, 0, totalLength);
        System.out.println(count);
    }

    public void pick(boolean[] av, int nextInd, boolean[] picked, int pickedCount, int goal) {
        if (goal == pickedCount) {
            permutate(picked, new StringBuilder(), goal);
            return;
        }
        if (nextInd >= av.length) {
            return;
        }

        // haara: ei oteta lukua kohdassa nextInd
        pick(av, nextInd+1, picked, pickedCount, goal);

        // haara: otetaan jos mahdollista
        if (av[nextInd]) {
            picked[nextInd] = true;
            pick(av, nextInd+1, picked, pickedCount+1, goal);
            // peruutetaan rekursio
            picked[nextInd] = false;
        }
    }

    public void permutate(boolean[] remaining, StringBuilder sb, int goal) {
        if (sb.length() == goal) {
            String both = sb.toString();
            String hoursString = sb.substring(0, maxHoursLength);
            String minutesString = sb.substring(maxHoursLength);
            long hours = Long.parseLong(hoursString);
            long minutes = Long.parseLong(minutesString);
            if (hours > lastHourToDisplay) return;
            if (minutes > lastMinuteToDisplay) return;
            count++;
            return;
        }
        for (int i=0; i<remaining.length; i++) {
            if (!remaining[i]) continue;
            sb.append(i);
            remaining[i] = false;
            permutate(remaining, sb, goal);
            remaining[i] = true;
            sb.deleteCharAt(sb.length() - 1);
        }
    }

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

        public Pair(long val, int id) {
            this.val = val;
            this.id = id;
        }

        @Override
        public int compareTo(Pair o) {
            if (this.val < o.val) return -1;
            if (this.val > o.val) return 1;
            return this.id - o.id;
        }
    }

    private List<Integer>[] toGraph(CIO io, int n) {
        List<Integer>[] g = new ArrayList[n+1];
        for (int i=1; i<=n; i++) g[i] = new ArrayList<>();
        for (int i=1; i<=n-1; i++) {
            int a = io.nextInt();
            int b = io.nextInt();
            g[a].add(b);
            g[b].add(a);
        }
        return g;
    }
}

class CIO extends PrintWriter {
    private InputStreamReader r;
    private static final int BUFSIZE = 1 << 15;
    private char[] buf;
    private int bufc;
    private int bufi;
    private StringBuilder sb;

    public CIO() {
        super(new BufferedOutputStream(System.out));
        r = new InputStreamReader(System.in);

        buf = new char[BUFSIZE];
        bufc = 0;
        bufi = 0;
        sb = new StringBuilder();
    }

    private void fillBuf() throws IOException {
        bufi = 0;
        bufc = 0;
        while(bufc == 0) {
            bufc = r.read(buf, 0, BUFSIZE);
            if(bufc == -1) {
                bufc = 0;
                return;
            }
        }
    }

    private boolean pumpBuf() throws IOException {
        if(bufi == bufc) {
            fillBuf();
        }
        return bufc != 0;
    }

    private boolean isDelimiter(char c) {
        return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f';
    }

    private void eatDelimiters() throws IOException {
        while(true) {
            if(bufi == bufc) {
                fillBuf();
                if(bufc == 0) throw new RuntimeException("IO: Out of input.");
            }

            if(!isDelimiter(buf[bufi])) break;
            ++bufi;
        }
    }

    public String next() {
        try {
            sb.setLength(0);

            eatDelimiters();
            int start = bufi;

            while(true) {
                if(bufi == bufc) {
                    sb.append(buf, start, bufi - start);
                    fillBuf();
                    start = 0;
                    if(bufc == 0) break;
                }

                if(isDelimiter(buf[bufi])) break;
                ++bufi;
            }

            sb.append(buf, start, bufi - start);

            return sb.toString();
        } catch(IOException e) {
            throw new RuntimeException("IO.next: Caught IOException.");
        }
    }

    public int nextInt() {
        try {
            int ret = 0;

            eatDelimiters();

            boolean positive = true;
            if(buf[bufi] == '-') {
                ++bufi;
                if(!pumpBuf()) throw new RuntimeException("IO.nextInt: Invalid int.");
                positive = false;
            }

            boolean first = true;
            while(true) {
                if(!pumpBuf()) break;
                if(isDelimiter(buf[bufi])) {
                    if(first) throw new RuntimeException("IO.nextInt: Invalid int.");
                    break;
                }
                first = false;

                if(buf[bufi] >= '0' && buf[bufi] <= '9') {
                    if(ret < -214748364) throw new RuntimeException("IO.nextInt: Invalid int.");
                    ret *= 10;
                    ret -= (int)(buf[bufi] - '0');
                    if(ret > 0) throw new RuntimeException("IO.nextInt: Invalid int.");
                } else {
                    throw new RuntimeException("IO.nextInt: Invalid int.");
                }

                ++bufi;
            }

            if(positive) {
                if(ret == -2147483648) throw new RuntimeException("IO.nextInt: Invalid int.");
                ret = -ret;
            }

            return ret;
        } catch(IOException e) {
            throw new RuntimeException("IO.nextInt: Caught IOException.");
        }
    }

    public long nextLong() {
        try {
            long ret = 0;

            eatDelimiters();

            boolean positive = true;
            if(buf[bufi] == '-') {
                ++bufi;
                if(!pumpBuf()) throw new RuntimeException("IO.nextLong: Invalid long.");
                positive = false;
            }

            boolean first = true;
            while(true) {
                if(!pumpBuf()) break;
                if(isDelimiter(buf[bufi])) {
                    if(first) throw new RuntimeException("IO.nextLong: Invalid long.");
                    break;
                }
                first = false;

                if(buf[bufi] >= '0' && buf[bufi] <= '9') {
                    if(ret < -922337203685477580L) throw new RuntimeException("IO.nextLong: Invalid long.");
                    ret *= 10;
                    ret -= (long)(buf[bufi] - '0');
                    if(ret > 0) throw new RuntimeException("IO.nextLong: Invalid long.");
                } else {
                    throw new RuntimeException("IO.nextLong: Invalid long.");
                }

                ++bufi;
            }

            if(positive) {
                if(ret == -9223372036854775808L) throw new RuntimeException("IO.nextLong: Invalid long.");
                ret = -ret;
            }

            return ret;
        } catch(IOException e) {
            throw new RuntimeException("IO.nextLong: Caught IOException.");
        }
    }

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

}