#include <bits/stdc++.h>
using namespace std;
char s[200010];
int main() {
  scanf("%s", (s));
  long long(n);
  scanf("%lld", &n);
  int l = strlen(s);
  long long cur = 0, ans = n, tominus = 1;
  for (long long i = 0; i < l; ++i) {
    cur = cur * 10 + s[i] - '0';
    cur %= n;
    if (i != 0) tominus = tominus * 10 % n;
  }
  for (long long i = 0; i < l; ++i) {
    if (s[i] != '0') ans = min(cur, ans);
    cur = cur - tominus * (s[i] - '0');
    cur *= 10;
    cur += s[i] - '0';
    cur %= n;
    cur = (cur + n) % n;
  }
  printf("%lld\n", ans);
}
