#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 5;
const int mod = 1e9 + 7;
const long long INF = 4e18 + 5;
char second[200200];
int n, m;
long long pw[200200], pref[200200], suff[200200];
int main() {
  scanf("%s %d", second + 1, &m);
  n = strlen(second + 1);
  pw[0] = 1;
  for (int i = 1; i <= n; ++i) {
    pw[i] = pw[i - 1] * 10 % m;
  }
  for (int i = n; i >= 1; --i) {
    int dig = second[i] - '0';
    suff[i] = ((dig * pw[n - i] % m) + suff[i + 1]) % m;
  }
  long long ans = inf;
  for (int i = 1; i <= n; ++i) {
    int dig = second[i] - '0';
    if (dig != 0) ans = min(ans, ((suff[i] * pw[i - 1]) % m + pref[i - 1]) % m);
    pref[i] = (pref[i - 1] * 10 + dig) % m;
  }
  cout << ans;
  return 0;
}
