#include <bits/stdc++.h>
using namespace std;
int DSY;
int val[400000 + 10];
long long qp(long long v, long long q) {
  if (v == 0) return 0;
  long long rtn = 1;
  while (q) {
    if (q & 1) {
      rtn *= v;
      rtn %= DSY;
    }
    v *= v;
    v %= DSY;
    q >>= 1;
  }
  return rtn;
}
int main(void) {
  ios::sync_with_stdio(false);
  string ipt;
  cin >> ipt >> DSY;
  int n = ipt.size();
  for (int i = 0; i < n; i++) {
    val[i] = val[i + n] = (ipt[i] - '0');
  }
  long long hval = qp(10, n - 1);
  long long ans = DSY;
  long long nov = 0;
  for (int i = 0; i < n; i++) {
    nov *= 10;
    nov += val[i];
    nov %= DSY;
  }
  ans = nov;
  for (int i = n; i < 2 * n - 1; i++) {
    nov -= (val[i - n] * hval) % DSY;
    nov += DSY;
    nov *= 10;
    nov += val[i];
    nov %= DSY;
    if (val[i - n + 1]) ans = min(ans, nov);
  }
  cout << ans;
  return 0;
}
