#include <bits/stdc++.h>
using namespace std;
const int maxn = 400010;
char s[maxn];
int dig[maxn];
int m, len;
int div_99(int lon) {
  int now = 1;
  for (int i = 1; i <= lon; ++i) {
    now = (10ll * now % m + 0) % m;
  }
  return now;
}
int div_num() {
  int now = 0;
  for (int i = 1; i <= len; ++i) {
    now = (10ll * now % m + dig[i]) % m;
  }
  return now;
}
int main() {
  scanf("%s%d", s + 1, &m);
  len = strlen(s + 1);
  for (int i = len; i; --i) {
    dig[i] = s[i] - '0';
  }
  int rem = div_99(len - 1);
  int sta = div_num(), ans = (int)1e9;
  ans = min(ans, sta);
  for (int i = 1; i < len; ++i) {
    if (dig[i]) {
      sta = (sta + m - 1ll * dig[i] * rem % m) % m;
    }
    sta = 10ll * sta % m;
    if (dig[i]) {
      sta = (sta + dig[i]) % m;
    }
    if (dig[i + 1]) {
      ans = min(ans, sta);
    }
  }
  printf("%d\n", ans);
  return 0;
}
