#include <bits/stdc++.h>
using namespace std;
char c[200001];
long long n, m, ans, bin, Mod;
int main() {
  scanf("%s", c);
  scanf("%I64d", &m);
  while (c[n] >= '0' && c[n] <= '9') Mod = (Mod * 10 + c[n] - '0') % m, ++n;
  bin = 1;
  for (int i = 1; i < n; ++i) bin = (bin * 10) % m;
  ans = Mod;
  for (int i = 0; i < n - 1; ++i) {
    long long x = c[i] - '0', y = c[i + 1] - '0';
    Mod = (Mod - bin * x % m) * 10 + x;
    Mod %= m;
    Mod = (Mod + m) % m;
    if (y && Mod < ans) ans = Mod;
  }
  printf("%I64d", ans);
  return 0;
}
