#include <bits/stdc++.h>
char s[200005];
int sz, i;
long long a, v[200005], A[200005], B[200005], ans;
int main() {
  scanf("%s", s);
  scanf("%lld", &a);
  sz = strlen(s);
  v[0] = 1LL;
  i = 1;
  while (i <= sz) v[i] = v[i - 1] * 10LL, v[i] %= a, i++;
  i = 1;
  while (i <= sz) {
    A[i] = A[i - 1] * 10LL + (s[i - 1] - '0');
    A[i] %= a;
    i++;
  }
  i = sz;
  while (i > 0) {
    B[i] = v[sz - i] * (s[i - 1] - '0') + B[i + 1];
    B[i] %= a;
    i--;
  }
  s[sz] = '1';
  i = 1, ans = A[sz];
  while (i < sz) {
    if (s[i] != '0' && ans > ((B[i + 1] * v[i]) % a + A[i]) % a)
      ans = ((B[i + 1] * v[i]) % a + A[i]) % a;
    i++;
  }
  printf("%lld", ans);
}
