#include <bits/stdc++.h>
using namespace std;
struct vl {
  long long x, y;
  bool operator<(const vl& t) const {
    if (x == t.x) return y < t.y;
    return x < t.x;
  }
};
const long long INF = 1e18;
const long long MAXN = 1e6 + 69;
long long q = 1, m, n, x, y, z, k, l;
long long a[MAXN];
string s;
long long luythua(long long x, long long y, long long mod) {
  if (y == 0) return 1;
  long long k = luythua(x, y / 2, mod);
  if (y % 2 == 0) return k * k % mod;
  return k * k % mod * x % mod;
}
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s;
  cin >> m;
  n = s.length();
  long long hs = luythua(10, n - 1, m);
  s += s;
  long long ans = INF;
  long long res = 0;
  for (long long i = 0; i < n; i++) {
    res *= 10;
    res %= m;
    res += s[i] - '0';
    res %= m;
  }
  ans = res;
  long long mod = m;
  for (long long i = n; i <= 2 * n - 1; i++) {
    res = (((res - (s[i - n] - '0') * hs) % mod) + mod) % mod;
    res *= 10;
    res %= mod;
    res += s[i] - '0';
    res %= mod;
    if (s[i - n + 1] != '0') ans = min(ans, res);
  }
  cout << ans;
}
