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.Random;

public class ProblemC {
  
  public static void main(String[] args) throws IOException {
    init();
    new ProblemC().run();
    out.flush();
    out.close();
  }
  
  static void init() throws IOException {
    in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    out = new PrintWriter(new OutputStreamWriter(System.out));
    in.ordinaryChars(0, 65535);
    in.wordChars(0, 65535);
    in.whitespaceChars(' ', ' ');
    in.whitespaceChars('\n', '\n');
    in.whitespaceChars('\r', '\r');
  }
  
  // класс для пар
  class Pair<L, R> {
    private final L X;
    private final R Y;
    
    public Pair(L X, R Y) {
      this.X = X;
      this.Y = Y;
    }
    
    public L getX() {
      return X;
    }
    
    public R getY() {
      return Y;
    }
    
    @Override
    public int hashCode() {
      return X.hashCode() ^ Y.hashCode();
    }
    
    @Override
    public boolean equals(Object o) {
      if (!(o instanceof Pair)) return false;
      Pair pairo = (Pair) o;
      return X.equals(pairo.getX()) && Y.equals(pairo.getY());
    }
  }
  
  static final long INFL = 200000000000000000L;
  static final int INF = 2000000000;
  static final boolean DEBUG = true;
  
  static StreamTokenizer in;
  static PrintWriter out;
  
  static void print(String s) {
    print(s, 0);
  }
  
  static void print(String s, int debug) {
    if (debug == 0 || DEBUG) {
      out.print(s);
    }
  }
  
  static void println(String s) {
    println(s, 0);
  }
  
  static void println(String s, int debug) {
    if (debug == 0 || DEBUG) {
      out.println(s);
    }
  }
  
  static void printArray(int[] arr) {
    println(Arrays.toString(arr));
  }
  
  static void printArray(char[] arr) {
    println(Arrays.toString(arr));
  }
  
  static void printArray(String[] arr) {
    println(Arrays.toString(arr));
  }
  
  static void sort(int[] a) {
    Random rnd = new Random();
    for (int i = a.length - 1; i > 0; i--) {
      int index = rnd.nextInt(i);
      a[i] ^= a[index];
      a[index] ^= a[i];
      a[i] ^= a[index];
    }
    Arrays.sort(a);
  }
  
  static char[] inChars;
  static int inCharsInd;
  
  static String next() throws IOException {
    in.nextToken();
    return in.sval;
  }
  
  static char nextChar() throws IOException {
    while (inChars == null || inCharsInd >= inChars.length) {
      inChars = next().toCharArray();
      inCharsInd = 0;
    }
    return inChars[inCharsInd++];
  }
  
  static int nextInt() throws IOException {
    return Integer.valueOf(next());
  }
  
  static double nextDouble() throws IOException {
    return Double.valueOf(next());
  }
  
  static long nextLong() throws IOException {
    return Long.valueOf(next());
  }
  
  private void run() throws IOException {
    solve();
  }
  
  int K, L, M, N, P;
  int count = 0, lengthN = 1, lengthM = 1;
  int M7 = 0, N7 = 0;
  private void solve() throws IOException {
    N = nextInt();
    M = nextInt();
    if (N > 1) lengthN = (int)((Math.log(N-1))/Math.log(7)) + 1;
    if (M > 1) lengthM = (int)((Math.log(M-1))/Math.log(7)) + 1;

    if (lengthN + lengthM > 7){
      out.println(0);
      return;
    }

    int N1 = N-1, pow = 1;
    while (N1 > 0){
      N7 += pow*(N1%7);
      N1/=7; pow*=10;
    }

    int M1 = M-1;
    pow = 1;
    while (M1 > 0){
      M7 += pow*(M1%7);
      M1/=7; pow*=10;
    }


    ArrayList<Integer> nums = new ArrayList<Integer>(){{
      for (int i = 0; i < 7; i++) add(i);
    }};

    countNumber(nums, "");

    out.println(count);
  }


  private void countNumber(ArrayList<Integer> nums, String s){
    if (s.length() >= lengthM + lengthN){
      int first = Integer.valueOf(s.substring(0,lengthN));
      int second = Integer.valueOf(s.substring(lengthN));
      if (first <= N7 && second <= M7){
        count++;
//        System.out.println("s = " +s);
//        System.out.println("first = " + first);
//        System.out.println("second = " + second);
      }
    } else {
      for (int i = 0; i < nums.size(); i++) {
        ArrayList<Integer> newAl = new ArrayList<>();
        for (int j = 0; j < nums.size(); j++) {
          if (j != i) newAl.add(nums.get(j));
        }
        String newS = s + Integer.toString(nums.get(i));
        countNumber(newAl, newS);
      }
    }
  }
  
}
