#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 10;
const long long mod = 1e9 + 7;
int n, m, a[maxn], s[maxn], mi, ans;
char c[maxn];
int main() {
  int i, j;
  scanf("%s", c + 1);
  n = strlen(c + 1);
  for (i = 1; i <= n; i++) a[i] = c[i] - '0';
  cin >> m;
  s[n] = 1;
  for (i = n - 1; i >= 1; i--) s[i] = s[i + 1] * 10 % m;
  for (i = 1; i <= n; i++) ans = (ans + s[i] * a[i]) % m;
  mi = ans;
  for (i = 1; i < n; i++) {
    ans = (ans + m - s[1] * a[i] % m) % m;
    ans = (ans * 10 + a[i]) % m;
    if (a[i + 1]) mi = min(mi, ans);
  }
  cout << mi << "\n";
  return 0;
}
