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

@SuppressWarnings("unchecked")
public class P686C {

  int [] enc(int l, int [] p) {
    int [] e = new int [2];
    for (int i = 0; i < l; i++) {
      e[0] |= 1 << p[i];
      e[1] = e[1] * 7 + p[i];
    }
    return e;
  }

  int check(Map.Entry<Integer, Integer> x, int max, Map<Integer, Integer> p) {
    int ans = 0;
    for (Map.Entry<Integer, Integer> e : p.entrySet()) {
      if (e.getKey() >= max) {
        break;
      }
      if ((x.getValue() & e.getValue()) == 0) {
        ans++;
      }
    }
    return ans;
  }

  public void run() throws Exception {
    Map<Integer, Integer> [] p = new TreeMap [7];

    for (int i0 = 0; i0 < 7; i0++)
      for (int i1 = 0; i1 < 7; i1++)
        if (i1 != i0) for (int i2 = 0; i2 < 7; i2++)
          if ((i2 != i1) && (i2 != i0)) for (int i3 = 0; i3 < 7; i3++)
            if ((i3 != i2) && (i3 != i1) && (i3 != i0)) for (int i4 = 0; i4 < 7; i4++)
              if ((i4 != i3) && (i4 != i2) && (i4 != i1) && (i4 != i0)) for (int i5 = 0; i5 < 7; i5++)
                if ((i5 != i4) && (i5 != i3) && (i5 != i2) && (i5 != i1) && (i5 != i0)) for (int i6 = 0; i6 < 7; i6++)
                  if ((i6 != i5) && (i6 != i4) && (i6 != i3) && (i6 != i2) && (i6 != i1) && (i6 != i0)) {
                    for (int l = 0; l < 7; l++) {
                      if (p[l] == null) {
                        p[l] = new TreeMap();
                      }
                      int [] e = enc(l + 1, new int [] {i0, i1, i2, i3, i4, i5, i6});
                      p[l].put(e[1], e[0]);
                    }
                  }

    int mh = nextInt(), mm = nextInt();
    int hl = Math.min(Integer.toString(mh - 1, 7).length(), 7);
    int ml = Math.min(Integer.toString(mm - 1, 7).length(), 7);
    
    int ans = 0;
    for (Map.Entry<Integer, Integer> e : p[hl - 1].entrySet()) {
      if (e.getKey() >= mh) {
        break;
      }
      ans += check(e, mm, p[ml - 1]);
    }
    
    println(ans);
  }

  public static void main(String... args) throws Exception {
    br = new BufferedReader(new InputStreamReader(System.in));
    pw = new PrintWriter(new BufferedOutputStream(System.out));
    new P686C().run();
    br.close();
    pw.close();
    System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]");
  }

  static long startTime = System.currentTimeMillis();
  static BufferedReader br;
  static PrintWriter pw;
  StringTokenizer stok;

  String nextToken() throws IOException {
    while (stok == null || !stok.hasMoreTokens()) {
      String s = br.readLine();
      if (s == null) { return null; }
      stok = new StringTokenizer(s);
    }
    return stok.nextToken();
  }

  void print(byte b) { print("" + b); }
  void print(int i) { print("" + i); }
  void print(long l) { print("" + l); }
  void print(double d) { print("" + d); }
  void print(char c) { print("" + c); }
  void print(Object o) {
    if (o instanceof int[]) { print(Arrays.toString((int [])o));
    } else if (o instanceof long[]) { print(Arrays.toString((long [])o));
    } else if (o instanceof char[]) { print(Arrays.toString((char [])o));
    } else if (o instanceof byte[]) { print(Arrays.toString((byte [])o));
    } else if (o instanceof short[]) { print(Arrays.toString((short [])o));
    } else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o));
    } else if (o instanceof float[]) { print(Arrays.toString((float [])o));
    } else if (o instanceof double[]) { print(Arrays.toString((double [])o));
    } else if (o instanceof Object[]) { print(Arrays.toString((Object [])o));
    } else { print("" + o); }
  }
  void print(String s) { pw.print(s); }
  void println() { println(""); }
  void println(byte b) { println("" + b); }
  void println(int i) { println("" + i); }
  void println(long l) { println("" + l); }
  void println(double d) { println("" + d); }
  void println(char c) { println("" + c); }
  void println(Object o) { print(o); println(); }
  void println(String s) { pw.println(s); }
  int nextInt() throws IOException { return Integer.parseInt(nextToken()); }
  long nextLong() throws IOException { return Long.parseLong(nextToken()); }
  double nextDouble() throws IOException { return Double.parseDouble(nextToken()); }
  char nextChar() throws IOException { return (char) (br.read()); }
  String next() throws IOException { return nextToken(); }
  String nextLine() throws IOException { return br.readLine(); }
  int [] readInt(int size) throws IOException {
    int [] array = new int [size];
    for (int i = 0; i < size; i++) { array[i] = nextInt(); }
    return array;
  }
  long [] readLong(int size) throws IOException {
    long [] array = new long [size];
    for (int i = 0; i < size; i++) { array[i] = nextLong(); }
    return array;
  }
  double [] readDouble(int size) throws IOException {
    double [] array = new double [size];
    for (int i = 0; i < size; i++) { array[i] = nextDouble(); }
    return array;
  }
  String [] readLines(int size) throws IOException {
    String [] array = new String [size];
    for (int i = 0; i < size; i++) { array[i] = nextLine(); }
    return array;
  }

  int gcd(int a, int b) {
    return ((b > 0) ? gcd(b, a % b) : a);
  }
}