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.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

// Codeforces Round #359 (Div. 2)
// Robbers' watch
public class Problem_686_C {

	private void doSolve(PrintWriter out) {
		
		int n = nextInt();
		int m = nextInt();
		
		Integer[] nBase7 = changeBaseFrom10(n - 1, 7);
		Integer[] mBase7 = changeBaseFrom10(m - 1, 7);

		int digitsN = nBase7.length;
		int digitsM = mBase7.length;
		
		int totalDigits = digitsN + digitsM;
		if (totalDigits > 7) {
			out.println("0");
			return;
		}
		
		int counter = 0;
		LinkedList<Integer> stack = new LinkedList<Integer>();
		while (true) {
			if (stack.size() < totalDigits) {
				stack.addLast( findNextDigit(stack, 0) );
			}
			else {
				if ( isValid( stack.subList(0, digitsN), nBase7 )
						&& isValid( stack.subList(digitsN, totalDigits), mBase7 ) ) {
					counter++;
				}
				
				while (true) {
					int currentDigit = stack.removeLast();
					Integer nextDigit = findNextDigit(stack, currentDigit + 1);
					if (nextDigit != null) {
						stack.addLast(nextDigit);
						break;
					}
					if (stack.isEmpty()) {
						break; // we are done!
					}
				}
				
				if (stack.isEmpty()) {
					break;
				}
			}
		}
		out.println(counter);
	}
	
	private Integer findNextDigit(List<Integer> digits, int startFrom) {
		for (int i = startFrom; i < 7; i++) {
			boolean used = false;
			for (int j : digits) {
				if (j == i) {
					used = true;
					break;
				}
			}
			if (!used) {
				return i;
			}
		}
		return null;
	}
	
	private boolean isValid(List<Integer> digits, Integer[] maxValue) {
		for (int i = 0; i < digits.size(); i++) {
			if (digits.get(i) == maxValue[i]) {
				continue;
			}
			return maxValue[i] > digits.get(i);
		}
		return true;
	}
	
	private Integer[] changeBaseFrom10(int decimal, int base) {
		List<Integer> result = new LinkedList<Integer>();

		if (decimal == 0) {
			result.add(0);
		}
		else {
			while (decimal > 0) {
				result.add( decimal % base );
				decimal /= base;
			}
		}
		Collections.reverse(result);
		return result.toArray(new Integer[result.size()]);
	}
	
	
	/* ========================================================= */
	
    private InputReader inputReader;
    
    
    public static void main(String[] args)
    {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        PrintWriter out = new PrintWriter(outputStream);
        Problem_686_C solver = new Problem_686_C();
        solver.solve(1, new InputStreamReader(inputStream), out);
        out.close();
    }

    public int nextInt() {
    	return inputReader.nextInt();
    }
    
    public String next() {
    	return inputReader.next();
    }
    
    public void solve(int test, Reader in, PrintWriter out)
    {
    	inputReader = new InputReader(in);
    	doSolve(out);
    	
    }
    
    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(Reader reader) {
            this.reader = new BufferedReader(reader, 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

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

    }

}
