#include <bits/stdc++.h>
using namespace std;
long long p;
int n;
char ch[200000 + 10];
long long TEN[200000 + 10];
long long Ans = 0x7fffffff;
long long l[200000 + 10];
long long r[200000 + 10];
int main() {
  scanf("%s%I64d", ch + 1, &p);
  n = strlen(ch + 1);
  TEN[0] = 1;
  for (int i = 1; i <= n; i++) TEN[i] = TEN[i - 1] * 10 % p;
  for (int i = 1; i <= n; i++) {
    l[i] = (l[i - 1] * 10 + ch[i] - '0') % p;
  }
  for (int i = n; i; i--) {
    r[i] = (r[i + 1] + TEN[n - i] * (ch[i] - '0')) % p;
  }
  for (int i = 1; i <= n; i++) {
    if (ch[i + 1] != '0') Ans = min(Ans, (l[i] + r[i + 1] * TEN[i]) % p);
  }
  printf("%I64d\n", Ans);
}
