#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  string s;
  cin >> s;
  long long n;
  cin >> n;
  vector<long long> v(s.size());
  long long base = 1;
  long long sum = 0;
  for (long long i = s.size() - 1; i >= 0; i--) {
    v[i] = (s[i] - '0') * base % n;
    sum += v[i];
    sum %= n;
    base *= 10;
    base %= n;
  }
  long long mn = sum;
  long long base2 = 1;
  s.push_back('0');
  for (long long i = 0; i < s.size() - 1; i++) {
    sum = (sum - v[i] * base2 % n + n) % n;
    sum *= 10;
    sum %= n;
    sum = (sum + (s[i] - '0')) % n;
    if (s[i + 1] != '0') {
      mn = min(sum, mn);
    }
    base2 *= 10;
    base2 %= n;
  }
  cout << mn << '\n';
  return 0;
}
