#include <bits/stdc++.h>
using namespace std;
int mod10[2000005];
int main() {
  string k;
  int m;
  cin >> k >> m;
  int n = k.length();
  mod10[0] = 1;
  mod10[1] = 10 % m;
  for (int i = 2; i < n; i++) {
    mod10[i] = (mod10[i - 1] * mod10[1]) % m;
  }
  int rem = 0;
  int best = m;
  for (int i = 0; i < n; i++) {
    int digit = (k[n - i - 1] - '0') % m;
    rem += (mod10[i] * digit) % m;
    rem %= m;
  }
  best = min(best, rem);
  for (int i = 0; i < n; i++) {
    int digit = (k[i] - '0') % m;
    int sub = (mod10[n - 1] * digit) % m;
    if (false)
      cerr << "digit"
           << " = " << digit << endl;
    if (false)
      cerr << "sub"
           << " = " << sub << endl;
    rem = (rem + m - sub) % m;
    rem = (rem * mod10[1]) % m;
    rem = (rem + digit) % m;
    if (false)
      cerr << "rem"
           << " = " << rem << endl;
    if (k[(i + 1) % n] != '0') best = min(best, rem);
  }
  cout << best << endl;
}
