#include <bits/stdc++.h>
using namespace std;
const int maxn = 200100;
using namespace std;
int q, mod, n;
char s[maxn];
int md[maxn];
void exgcd(long long a, long long b, long long& d, long long& x, long long& y) {
  if (!b) {
    d = a;
    x = 1;
    y = 0;
  } else {
    exgcd(b, a % b, d, y, x);
    y -= x * (a / b);
  }
}
long long inv(long long a, long long p) {
  long long d, x, y;
  exgcd(a, p, d, x, y);
  return d == 1 ? (x + p) % p : -1;
}
void solve() {
  cin >> s;
  cin >> mod;
  n = (int)strlen(s);
  int xxx = inv(10, mod);
  int ans = 0;
  md[0] = 1;
  for (int i = 1; i <= n; i++) md[i] = md[i - 1] * 10 % mod;
  int dd = 1;
  for (int i = n - 1; i >= 0; i--) {
    ans = ans + (s[i] - '0') * dd;
    ans %= mod;
    dd = dd * 10 % mod;
  }
  int last = ans;
  for (int i = 0; i < n; i++) {
    int tp = last;
    tp = (tp - (s[i] - '0') * md[n - 1]) % mod;
    tp = tp * 10 % mod;
    tp += s[i] - '0';
    tp %= mod;
    if (tp < 0) tp += mod;
    int c = i + 1;
    if (c == n) c = 0;
    if (s[c] != '0') ans = min(ans, tp);
    last = tp;
  }
  cout << ans;
}
int main() {
  solve();
  return 0;
}
