#include <bits/stdc++.h>
using namespace std;
const int MAXN = -1;
long long expmod(long long b, long long e, long long m) {
  long long ans = 1;
  while (e) {
    if (e & 1) ans = ans * b % m;
    b = b * b % m;
    e >>= 1;
  }
  return ans;
}
int n, m;
string S;
int main() {
  while (cin >> S) {
    n = ((int)S.size());
    scanf("%d", &m);
    long long act = 0;
    for (int i = (0); i < (int)(n); i++) act = (act * 10 + S[i] - '0') % m;
    long long ans = act, lp = expmod(10, n, m);
    for (int i = (0); i < (int)(n); i++) {
      int d = S[i] - '0';
      act = act * 10 % m;
      act = (act - d * lp % m + d + m) % m;
      if (S[(i + 1) % n] != '0') ans = min(ans, act);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
