#include <bits/stdc++.h>
using namespace std;
const int maxn = 200100;
char s[maxn];
int mod, res, ans = 0x7f7f7f7f;
int main() {
  scanf("%s", &s);
  scanf("%d", &mod);
  int n = strlen(s);
  res = 1;
  for (int i = 1; i < n; ++i) {
    res = res * 10 % mod;
  }
  int now = 0;
  for (int i = 0; i < n; ++i) {
    now = (now * 10 % mod + (s[i] - '0')) % mod;
  }
  ans = min(ans, now % mod);
  for (int i = 1; i < n; ++i) {
    int x = s[i - 1] - '0';
    now = (now - x * res % mod + mod) % mod;
    now = (now * 10 % mod + x) % mod;
    if (s[i] == '0') continue;
    ans = min(ans, now);
  }
  printf("%d\n", ans);
  return 0;
}
