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).
import java.io.*;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

public class Problem implements Runnable {

    int[] a = new int[7];
    int[] ma;
    int ll;
    int c;
    int nl;
    int ml;
    void rec(int l) {
        if (ll - l == nl) {
            int sum = 0;
            for (int i = 0; i < nl; i++) {
                sum *= 7;
                sum += ma[i];
            }
            if (sum >= n) {
                return;
            }
        }
        if (l == 0) {
            int sum = 0;
            for (int i = nl; i < ll; i++) {
                sum *= 7;
                sum += ma[i];
            }
            if (sum < m) {
                c++;
            }
            return;
        }
        for (int i = 0; i < 7; i++) {
            if (a[i] == 0) {
                a[i]++;
                ma[ll - l] = i;
                rec(l - 1);
                a[i]--;
            }
        }
    }

    int n, m;
    private void solve() throws IOException {
        n = io.readInt();
        m = io.readInt();
        if ((Integer.toString(n - 1, 7) + Integer.toString(m - 1, 7)).length() > 7) {
            io.println(0);
        } else {
            nl = Integer.toString(n - 1, 7).length();
            ml = Integer.toString(m - 1, 7).length();
            ll = nl + ml;
            ma = new int[ll];
            c = 0;
            rec(ll);
            io.println(c);
//            int c = 0;
//            int[] a = new int[7];
//            outer: for (int i = 0; i < n; i++) {
//                for (int j = 0; j < 7; j++) {
//                    a[j] = 0;
//                }
//                String s = Integer.toString(i, 7);
//                for (int j = 0; j < s.length(); j++) {
//                    int x = s.charAt(j) - '0';
//                    a[x]++;
//                    if (a[x] > 1) {
//                        continue outer;
//                    }
//                }
//                outer2: for (int j = 0; j < m; j++) {
//                    s = Integer.toString(j, 7);
//                    for (int k = 0; k < s.length(); k++) {
//                        int x = s.charAt(k) - '0';
//                        a[x]++;
//                        if (a[x] > 1) {
//                            continue outer2;
//                        }
//                    }
//                    c++;
//                }
//            }
//            io.println(c);
        }
    }

    private static FastIO io;
    private static final String FILE_NAME = null;

    public void run() {
        try (FastIO fastIO = new FastIO(FILE_NAME)) {
            io = fastIO;
            solve();
        } catch (IOException e) {
            throw new NullPointerException();
        }
    }

    public static void main(String[] args) {
        new Thread(new Problem()).start();
    }
}

class Algo {
    static void shuffleArray(int[] a) {
        Random rand = ThreadLocalRandom.current();
        for (int i = a.length - 1; i >= 0; i--) {
            int ind = rand.nextInt(i + 1);
            int t = a[i];
            a[i] = a[ind];
            a[ind] = t;
        }
    }
}

class FastIO implements AutoCloseable {
    private BufferedReader in;
    private PrintWriter out;
    private StringTokenizer st;

    public FastIO() {
        in = new BufferedReader(new InputStreamReader(System.in));
        out = new PrintWriter(System.out);
    }

    FastIO(String file) throws FileNotFoundException {
        if (file == null) {
            in = new BufferedReader(new InputStreamReader(System.in));
            out = new PrintWriter(System.out);
        } else {
            in = new BufferedReader(new FileReader(file + ".in"));
            out = new PrintWriter(file + ".out");
        }
    }

    void flush() {
        out.flush();
    }

    String readLine() throws IOException {
        st = null;
        return in.readLine();
    }

    String readToken() throws IOException {
        return nextToken();
    }

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

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

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

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

    void print(Object o) {
        out.print(o.toString());
    }

    void println(Object o) {
        out.println(o.toString());
    }

    @Override
    public void close() throws IOException {
        in.close();
        out.close();
    }
}