#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n;
long long m;
char s[N];
int main() {
  cin >> (s + 1) >> m;
  n = strlen(s + 1);
  long long curr = 0, pw = 1;
  for (int i = 1; i <= n; ++i) {
    curr = (curr * 10 + s[i] - '0') % m;
    pw = (pw * 10) % m;
  }
  long long ans = m;
  for (int i = 1; i <= n; ++i) {
    if (s[i] != '0') ans = min(ans, curr);
    curr = ((curr * 10 + m - (s[i] - '0') * pw % m) % m + s[i] - '0') % m;
  }
  printf("%I64d\n", ans);
  return 0;
}
