#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
long long qpow(long long a, long long b, long long c) {
  long long ret = 1;
  for (; b; (a *= a) %= c, b >>= 1)
    if (b & 1) (ret *= a) %= c;
  return ret;
}
string s;
long long x = 0, res = INF;
int n, m;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> s >> m, n = s.size();
  for (int i = 0; i < n; i++) x = (x * 10 + (s[i] - '0')) % m;
  res = x;
  long long pow_ = qpow(10, n - 1, m);
  for (int i = 0; i < n - 1; i++) {
    (x += m - pow_ * (s[i] - '0') % m) %= m;
    x = (x * 10 + s[i] - '0') % m;
    if (s[i + 1] != '0') {
      if (x < res) res = x;
    }
  }
  cout << res << endl;
  return 0;
}
