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.math.BigInteger;
import java.util.*;

public class Main {
    private FastScanner in;
    private PrintWriter out;
//    private Timer timer = new Timer();

    int getLength(int x) {
        if (x == 0) return 1;
        int res = 0;
        while (x > 0) {
            res++;
            x /= 7;
        }
        return res;
    }

    void inc(int[] h) {
        h[0]++;
        for (int i = 0; i < h.length - 1; i++) {
            if (h[i] == 7) {
                h[i] = 0;
                h[i + 1]++;
            }
        }
    }

    private void solve() throws IOException {
        int[] p7 = new int[10];
        p7[0] = 1;
        for (int i = 1; i < p7.length; i++) {
            p7[i] = p7[i - 1] * 7;
        }
        int n = in.nextInt();
        int k = in.nextInt();
        int hr = getLength(n - 1);
        int mr = getLength(k - 1);
        if (hr + mr > 7) {
            out.println(0);
            return;
        }
        TreeMap<String, Integer> dp1 = new TreeMap<>();
        int[] h = new int[hr];
        for (int i = 0; i < n; i++) {
            TreeSet<Integer> ts = new TreeSet<>();
            for (int j = 0; j < hr; j++) {
                ts.add(h[j]);
            }
            inc(h);
            if (ts.size() != hr) continue;
            StringBuilder cur = new StringBuilder();
            for (Integer t : ts) {
                cur.append((char) (t + '0'));
            }
            dp1.put(cur.toString(), dp1.getOrDefault(cur.toString(), 0) + 1);
        }
        TreeMap<String, Integer> dp2 = new TreeMap<>();
        int[] m = new int[mr];
        long ans = 0;
        for (int i = 0; i < k; i++) {
            TreeSet<Integer> ts = new TreeSet<>();
            for (int j = 0; j < mr; j++) {
                ts.add(m[j]);
            }
            inc(m);
            if (ts.size() != mr) continue;
            StringBuilder cur = new StringBuilder();
            for (Integer t : ts) {
                cur.append((char) (t + '0'));
            }
            dp2.put(cur.toString(), dp2.getOrDefault(cur.toString(), 0) + 1);
        }
        for (Map.Entry<String, Integer> e1 : dp1.entrySet()) {
            TreeSet<Character> cur = new TreeSet<>();
            for (char c : e1.getKey().toCharArray()) {
                cur.add(c);
            }
            loop:
            for (Map.Entry<String, Integer> e2 : dp2.entrySet()) {
                for (char e : e2.getKey().toCharArray()) {
                    if (cur.contains(e)) continue loop;
                }
                ans += e1.getValue() * e2.getValue();
            }
        }
        out.println(ans);
    }

    private void run() throws IOException {
        Locale.setDefault(Locale.US);
        in = new FastScanner();
        out = new PrintWriter(System.out);
        solve();
        out.close();
    }

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

class FastScanner {
    private final BufferedReader br;
    private StringTokenizer st;
    private String last;

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

    public FastScanner(String path) throws IOException {
        br = new BufferedReader(new FileReader(path));
    }

    public FastScanner(String path, String decoder) throws IOException {
        br = new BufferedReader(new InputStreamReader(new FileInputStream(path), decoder));
    }

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

    String nextLine() throws IOException {
        st = null;
        return (last == null) ? br.readLine() : last;
    }

    boolean hasNext() {
        if (st != null && st.hasMoreElements())
            return true;

        try {
            while (st == null || !st.hasMoreElements()) {
                last = br.readLine();
                st = new StringTokenizer(last);
            }
        } catch (Exception e) {
            return false;
        }

        return true;
    }

    String[] nextStrings(int n) throws IOException {
        String[] arr = new String[n];
        for (int i = 0; i < n; i++)
            arr[i] = next();
        return arr;
    }

    String[] nextLines(int n) throws IOException {
        String[] arr = new String[n];
        for (int i = 0; i < n; i++)
            arr[i] = nextLine();
        return arr;
    }

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

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

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

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

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

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

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

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

    double[] nextDoubles(int size) throws IOException {
        double[] arr = new double[size];
        for (int i = 0; i < size; i++)
            arr[i] = nextDouble();
        return arr;
    }

    boolean nextBool() throws IOException {
        String s = next();
        if (s.equalsIgnoreCase("true") || s.equals("1"))
            return true;

        if (s.equalsIgnoreCase("false") || s.equals("0"))
            return false;

        throw new IOException("Boolean expected, String found!");
    }
}