#include <bits/stdc++.h>
using namespace std;
const int maxs = 1000050, maxss = maxs * 10, mm = 1e7, mm1 = 1e6, mm2 = 1e5;
long long n, K, ans = -1;
int num1[maxs], s1 = 0, num2[maxs], s2 = 0;
int hs = 0, he[maxss], ne[maxs], cnt[maxs];
long long st[maxs];
long long chg(int x, int y, long long z) { return (z * 10 + x) * 10 + y; }
void push(long long sta) {
  int i, st0 = sta % maxss;
  for (i = he[st0]; i; i = ne[i])
    if (st[i] == sta) {
      cnt[i]++;
      return;
    }
  ne[++hs] = he[st0], he[st0] = hs, st[hs] = sta, cnt[hs]++;
}
int query(long long sta) {
  int i, st0 = sta % maxss;
  for (i = he[st0]; i; i = ne[i])
    if (st[i] == sta) return cnt[i];
  return 0;
}
void dfs(int x, int d) {
  if (d >= 7) {
    if (x) num1[s1++] = x;
    if (x / mm1 || (x / 100000 && x / 100000 > x / 10000 % 10)) num2[s2++] = x;
    return;
  }
  int i;
  if (x < 10) {
    for (i = 0; i <= 9; i++)
      if (!x || i != x) dfs(x * 10 + i, d + 1);
  } else if (x / 10 % 10 > x % 10)
    for (i = x % 10 + 1; i <= 9; i++) dfs(x * 10 + i, d + 1);
  else if (x / 10 % 10 < x % 10)
    for (i = 0; i < x % 10; i++) dfs(x * 10 + i, d + 1);
}
int main() {
  int i, j, k, i2;
  bool z = 0;
  scanf("%I64d%I64d", &n, &K);
  dfs(0, 0);
  for (i = 0; i < s2; i++)
    push(chg(num2[i] / mm1, num2[i] / mm2 % 10, num2[i] % n));
  for (i = 0; i < s1; i++)
    if (!(num1[i] % n) && !--K) {
      ans = num1[i];
      break;
    }
  if (K) {
    for (i = 0; i < s1; i++) {
      long long rem = (n - (long long)num1[i] * mm % n) % n;
      int tt = num1[i] % 10,
          t = (!(num1[i] / 10) ? 0 : (num1[i] / 10 % 10 > tt ? 1 : -1));
      for (j = 0; j < 10; j++) {
        for (k = 0; k < 10; k++)
          if ((t > 0 && tt < j && j > k) || (t < 0 && tt > j && j < k) ||
              (!t && tt != j && j != k && (tt > j) != (j > k))) {
            int ts = query(chg(j, k, rem));
            if (K <= ts) {
              z = 1;
              break;
            }
            K -= ts;
          }
        if (z) break;
      }
      if (z) {
        for (i2 = 0; i2 < s2; i2++)
          if (num2[i2] / mm1 == j && num2[i2] / mm2 % 10 == k &&
              num2[i2] % n == rem && !(--K)) {
            ans = (long long)num1[i] * mm + num2[i2];
            break;
          }
        break;
      }
    }
  }
  printf("%I64d\n", ans);
  return 0;
}
