#include <bits/stdc++.h>
using namespace std;
const int maxn = 200005;
char cc[maxn];
long long aa[maxn];
long long x[maxn];
long long m;
long long ans;
long long minn;
int main(void) {
  scanf("%s", cc);
  int ios = 0;
  while (cc[ios] >= '0' && cc[ios] <= '9') {
    aa[ios] = cc[ios] - '0';
    ios++;
  }
  scanf("%I64d", &m);
  x[ios - 1] = 1;
  for (int i = ios - 2; i >= 0; i--) {
    x[i] = x[i + 1] * 10;
    x[i] %= m;
  }
  minn = m;
  for (int i = 0; i < ios; i++) {
    ans += (aa[i] * x[i]) % m;
  }
  ans = ((ans % m) + m) % m;
  minn = min(minn, ans);
  for (int i = 0; i < ios; i++) {
    ans -= (aa[i] * x[0]) % m;
    ans = (ans * 10) % m;
    ans += aa[i] % m;
    ans = ((ans % m) + m) % m;
    if (aa[i + 1] == 0) continue;
    minn = min(minn, ans);
  }
  cout << minn << endl;
  return 0;
}
