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.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;

public class Main {
    private static boolean IS_MULTI_PROBLEM = false;

    public static void main(String[] args) {
        Scanner in = new FastScanner();
        PrintWriter out = new PrintWriter(System.out);

        Solver solver = new Solver(in, out);

        for (int i = 0, n = IS_MULTI_PROBLEM ? in.nextInt() : 1; i < n; i++) {
            solver.solve();
        }

        out.flush();
    }
}

class Solver {
    private Scanner in;
    private PrintWriter out;

    Solver(Scanner in, PrintWriter out) {
        this.in = in;
        this.out = out;
    }

    long ans = 0;

    void calc(int n, int[] a1, int[] a2, String n7, String m7, int n7i, int m7i, Set<Integer> used) {
        for (int x = 0; x < 7; x++) {
            if (used.contains(x)) continue;
            used.add(x);
            if (n < n7.length()) a1[n] = x;
            else a2[n - n7.length()] = x;
            if (n < n7.length() + m7.length() - 1) {
                calc(n + 1, a1, a2, n7, m7, n7i, m7i, used);
            } else {
                int aa1 = 0;
                int aa2 = 0;
                for (int i = 0; i < a1.length; i++) {
                    aa1 += a1[i] * Math.pow(10, a1.length - i - 1);
                }
                for (int i = 0; i < a2.length; i++) {
                    aa2 += a2[i] * Math.pow(10, a2.length - i - 1);
                }
                // out.println("d " + aa1 + " " + n7i + " " + aa2 + " " + m7i);
                if (aa1 <= n7i && aa2 <= m7i) ans++;
                // if (aa1 <= n7i && aa2 <= m7i) out.println("d " + aa1 + " " + n7i + " " + aa2 + " " + m7i);
            }
            used.remove(x);
        }
    }

    void solve() {
        int n = in.nextInt();
        int m = in.nextInt();
        if (n < 7 && m < 7) {
            out.println((Math.max(n, m) - 1) * Math.min(n, m));
            return;
        }
        String n7 = Integer.toString(n - 1, 7);
        String m7 = Integer.toString(m - 1, 7);
        if (n7.length() + m7.length() > 7) {
            out.println(0);
            return;
        }
        calc(0, new int[n7.length()], new int[m7.length()], n7, m7, Integer.parseInt(n7), Integer.parseInt(m7), new HashSet());
        out.println(ans);
    }
}

interface Scanner {
    boolean hasNext();
    String next();
    long nextLong();
    int nextInt();
    double nextDouble();
}

class FastScanner implements Scanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        }else{
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }

    private int readByte() { if (hasNextByte()) return buffer[ptr++]; else return -1;}

    private static boolean isPrintableChar(int c) { return 33 <= c && c <= 126;}

    public boolean hasNext() { while(hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++; return hasNextByte();}

    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while(isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while(true){
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            }else if(b == -1 || !isPrintableChar(b)){
                return minus ? -n : n;
            }else{
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }

    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }

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

class UnionFind {
    int[] data;

    UnionFind(int size) {
        data = new int[size];
        Arrays.fill(data, -1);
    }

    boolean merge(int x, int y) {
        x = getRoot(x);
        y = getRoot(y);
        if (x != y) {
            if (data[y] < data[x]) {
                int t = x;
                x = y;
                y = t;
            }
            data[x] += data[y];
            data[y] = x;
        }
        return x != y;
    }

    boolean bothSameRoot(int x, int y) {
        return getRoot(x) == getRoot(y);
    }

    int getRoot(int x) {
        return data[x] < 0 ? x : (data[x] = getRoot(data[x]));
    }

    int getSize(int x) {
        return -data[getRoot(x)];
    }
}
