#include <bits/stdc++.h>
#pragma GCC optimize(2)
const int half = 1e7, quarter = 1e5;
long long n, k, mod, sum;
int digit[20], high, low, flag[half];
std::unordered_map<long long, int> lower[10], upper[10];
inline int log(int x) {
  int cnt = 0;
  while (x) digit[cnt++] = x % 10, x /= 10;
  return --cnt;
}
inline long long opp(long long x) {
  long long mod = x * half % n;
  return mod ? n - mod : 0;
}
int main() {
  std::cin >> n >> k;
  for (int x = 1; x < half; x++) {
    high = log(x), flag[x] = 0;
    for (int i = 1; i < high; i++)
      flag[x] |= (digit[i] > digit[i - 1] && digit[i] < digit[i + 1]),
          flag[x] |= (digit[i] < digit[i - 1] && digit[i] > digit[i + 1]);
    for (int i = 0; i < high; i++) flag[x] |= digit[i] == digit[i + 1];
    if (flag[x]) continue;
    if (mod = x % n, !(k -= !mod))
      return std::cout << x << std::endl, 0;
    else if (high == 5 && digit[5] > digit[4])
      ++upper[0][mod];
    else if (high == 6 && digit[6] > digit[5])
      ++lower[digit[6]][mod];
    else if (high == 6 && digit[6] < digit[5])
      ++upper[digit[6]][mod];
  }
  for (int x = 1; x < half; x++) {
    if (flag[x]) continue;
    high = log(x);
    if (mod = opp(x), high == 0) {
      for (int i = 0; i < digit[0]; i++)
        if (upper[i].find(mod) != upper[i].end()) sum += upper[i][mod];
      for (int i = 9; i > digit[0]; i--)
        if (lower[i].find(mod) != lower[i].end()) sum += lower[i][mod];
    } else if (digit[0] > digit[1]) {
      for (int i = 0; i < digit[0]; i++)
        if (upper[i].find(mod) != upper[i].end()) sum += upper[i][mod];
    } else if (digit[0] < digit[1]) {
      for (int i = 9; i > digit[0]; i--)
        if (lower[i].find(mod) != lower[i].end()) sum += lower[i][mod];
    }
    if (k > sum)
      k -= sum, sum = 0;
    else {
      low = high + 7;
      for (int i = 0; i <= high; i++) digit[i + 7] = digit[i];
      for (int y = quarter; y < half; y++) {
        if (y % n != mod)
          continue;
        else
          sum = 1ll * x * half + y;
        high = log(y), flag[0] = 0;
        while (high < 6) digit[++high] = 0;
        high = low;
        for (int i = 1; i < high; i++)
          flag[0] |= (digit[i] > digit[i - 1] && digit[i] < digit[i + 1]),
              flag[0] |= (digit[i] < digit[i - 1] && digit[i] > digit[i + 1]);
        for (int i = 0; i < high; i++) flag[0] |= digit[i] == digit[i + 1];
        if (flag[0])
          continue;
        else if (!--k)
          return std::cout << sum << std::endl, 0;
      }
    }
  }
  return std::cout << -1 << std::endl, 0;
}
