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.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    static MyScanner   in;
    static PrintWriter out;
//    static Timer timer = new Timer();

    static boolean[] bools = new boolean[8];

    public static void main(String[] args) throws IOException {
        in = new MyScanner();
        out = new PrintWriter(System.out, true);

        int h = in.nextInt(), m = in.nextInt();
        String sh = Integer.toString(h - 1, 7), sm = Integer.toString(m - 1, 7);

        if (sh.length() >= 7 || sm.length() >= 7) {
            out.println(0);
            return;
        }

        ArrayList<String> hour = new ArrayList<>();
        for (int i = 0; i < h; i++) {
            String s = Integer.toString(i, 7);
            if (s.length() + 1 < sh.length())
                continue;
            if (s.length() + 1 == sh.length())
                s = "0" + s;
            if (check(s))
                hour.add(s);
        }

        ArrayList<String> minute = new ArrayList<>();
        for (int i = 0; i < m; i++) {
            String s = Integer.toString(i, 7);
            if (s.length() + 1 < sm.length())
                continue;
            if (s.length() + 1 == sm.length())
                s = "0" + s;
            if (check(s))
                minute.add(s);
        }

        int cnt = 0;
        for (int i = 0; i < hour.size(); i++)
            for (int j = 0; j < minute.size(); j++)
                if (check(hour.get(i), minute.get(j))) {
                    cnt++;
//                    out.println(hour.get(i) + ":" + minute.get(j));
                }
        out.println(cnt);
    }

    static boolean check(String s1, String s2) {
        Arrays.fill(bools, false);
        for (int i = 0; i < s1.length(); i++) {
            if (bools[s1.charAt(i) - '0'])
                return false;
            bools[s1.charAt(i) - '0'] = true;
        }
        for (int i = 0; i < s2.length(); i++) {
            if (bools[s2.charAt(i) - '0'])
                return false;
            bools[s2.charAt(i) - '0'] = true;
        }

        return true;
    }

    static boolean check(String s) {
        Arrays.fill(bools, false);
        for (int i = 0; i < s.length(); i++) {
            if (bools[s.charAt(i) - '0'])
                return false;
            bools[s.charAt(i) - '0'] = true;
        }

        return true;
    }

}

class MyScanner {

    public static final String CP1251 = "cp1251";

    private final BufferedReader  br;
    private       StringTokenizer st;
    private       String          last;

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

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

    public MyScanner(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 next(String delim) throws IOException {
        while (st == null || !st.hasMoreElements())
            st = new StringTokenizer(br.readLine());
        last = null;
        return st.nextToken(delim);
    }

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

    double[][] next2Doubles(int n, int m) throws IOException {
        double[][] arr = new double[n][m];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                arr[i][j] = 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!");
    }
}
