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).
#include <bits/stdc++.h>
volatile bool isLocalTestEnabled = 0;
bool g_isLocalPrintEnabled = (bool)(0);
template <typename T>
void UpdateMin(T& a, const T b) {
  a = std::min(a, b);
}
template <typename T>
void UpdateMax(T& a, const T b) {
  a = std::max(a, b);
}
const long double Pi = std::atan(1.0L) * 4.0L;
static const long double Eps = 1.0e-09;
template <typename T>
bool IsEqual(const T a, const T b) {
  return std::abs(a - b) < Eps;
}
template <typename T>
bool IsGreater(const T a, const T b) {
  return a > b + Eps;
}
template <typename T>
bool IsLess(const T a, const T b) {
  return a + Eps < b;
}
template <typename T>
bool IsGreaterEqual(const T a, const T b) {
  return !IsLess(a, b);
}
template <typename T>
bool IsLessEqual(const T a, const T b) {
  return !IsGreater(a, b);
}
template <typename T>
std::string ToStr(const T& val) {
  std::ostringstream ostr;
  ostr << val;
  return ostr.str();
}
template <typename T>
bool FromStr(const std::string& str, T& val) {
  std::istringstream istr(str);
  istr >> val;
  return !!istr;
}
template <typename T>
std::istream& operator>>(std::istream& ist, std::vector<T>& data) {
  ;
  for (size_t i = 0; i < data.size(); i++) {
    ist >> data[i];
  }
  return ist;
}
template <typename T>
T Read(std::istream& ist) {
  ;
  T val;
  ist >> val;
  return val;
}
template <typename T>
std::ostream& operator<<(std::ostream& ost, const std::vector<T>& data) {
  for (size_t i = 0; i < data.size(); i++) {
    if (i != 0) {
      ost << ' ';
    }
    ost << data[i];
  }
  return ost;
}
template <size_t id>
class StopWatch {};
const uint32_t Base = 7;
uint32_t GetMaxMask(const size_t len) {
  uint32_t p = 1;
  for (size_t i = 1; i <= len; i++) {
    p *= Base;
  }
  return p;
}
std::vector<uint32_t> GetDigits(uint32_t v, const uint32_t vLen) {
  std::vector<uint32_t> digits(vLen);
  for (size_t i = 0; i < vLen; i++) {
    digits[vLen - i - 1] = v % Base;
    v /= Base;
  }
  return digits;
}
uint32_t DigitsCount(const uint32_t z) {
  if (z == 0) return 1;
  uint32_t c = z;
  uint32_t ans = 0;
  while (c != 0) {
    c /= Base;
    ans++;
  }
  return ans;
}
uint32_t ToValue(const std::vector<uint32_t>& digits, const size_t b,
                 const size_t e) {
  uint32_t v = 0;
  for (size_t i = b; i < e; i++) {
    v *= Base;
    v += digits[i];
  }
  return v;
}
void GetVariants(const uint32_t n, const uint32_t m, const uint32_t nLen,
                 const uint32_t mLen, const uint32_t len, const uint32_t mask,
                 std::set<std::pair<uint32_t, uint32_t> >& vars) {
  std::vector<uint32_t> digits;
  uint32_t rmask = mask;
  for (size_t i = 0; i < len; i++) {
    const uint32_t d = rmask % Base;
    digits.push_back(d);
    rmask /= Base;
  }
  std::reverse(digits.begin(), digits.end());
  const size_t zerosCount = std::count(digits.begin(), digits.end(), 0);
  if (zerosCount > 1) return;
  std::set<uint32_t> allDigits(digits.begin(), digits.end());
  if (allDigits.size() != digits.size()) return;
  for (size_t firstLen = 1; firstLen < len; firstLen++) {
    const size_t secondLen = len - firstLen;
    if (firstLen > nLen) continue;
    if (secondLen > mLen) continue;
    const uint32_t x = ToValue(digits, 0, firstLen);
    const uint32_t y = ToValue(digits, firstLen, len);
    if (x >= n) continue;
    if (y >= m) continue;
    vars.insert(std::pair<uint32_t, uint32_t>(x, y));
  }
}
bool Solve(std::istream& ist, std::ostream& ost, const bool multipleTestMode) {
  StopWatch<1> sw;
  (void)sw;
  uint32_t n, m;
  ist >> n >> m;
  if (multipleTestMode && !ist) return false;
  if (!g_isLocalPrintEnabled) {
  } else
    std::cerr << std::endl << "Next test: " << n << ' ' << m << std::endl;
  const size_t nLen = DigitsCount(n - 1);
  const size_t mLen = DigitsCount(m - 1);
  std::set<std::pair<uint32_t, uint32_t> > vars;
  for (uint32_t len = nLen + mLen; len <= Base; len++) {
    const uint32_t MaxMask = GetMaxMask(len);
    for (uint32_t mask = 0; mask < MaxMask; mask++) {
      GetVariants(n, m, nLen, mLen, len, mask, vars);
    }
  }
  const size_t ans = vars.size();
  ost << ans << std::endl;
  return multipleTestMode;
}
int main(int argc, const char* argv[]) {
  std::ios_base::sync_with_stdio(false);
  std::istream* ist = &std::cin;
  std::ostream* ost = &std::cout;
  std::unique_ptr<std::istream> fileInput;
  if (argc > 1) {
    fileInput.reset(new std::ifstream(argv[1]));
    if (!(*fileInput)) {
      std::cout << "File not found: " << argv[1] << std::endl;
    }
    ist = fileInput.get();
  }
  Solve(*ist, *ost, false);
}
