#include <bits/stdc++.h>
#pragma GCC optimize("inline")
using namespace std;
const int N = 1e6 + 7;
long long t, x, p, k, m, s1[N], f[N], ans, sum;
long long modulo(long long a, long long b, long long n) {
  long long x = 1, y = a;
  while (b > 0) {
    if (b % 2 == 1) {
      x = (x * y) % n;
    }
    y = (y * y) % n;
    b /= 2;
  }
  return x % n;
}
int main() {
  string s;
  long long m, l;
  cin >> s >> m;
  l = m;
  int n = s.size();
  for (int i = 0; i < n; i++) {
    s1[i] = s[i] - '0';
    sum *= 10;
    sum += s1[i];
    sum %= m;
  }
  for (int i = 0; i < n; i++) {
    if (s1[i] != 0) l = min(l, sum);
    long long k = (s1[i] * modulo(10, n - 1, m)) % m;
    sum -= k;
    sum = (sum + m) % m;
    sum = (sum * 10 + s1[i]) % m;
  }
  cout << l;
}
