#include <bits/stdc++.h>
using namespace std;
long long deg[200100];
int main() {
  string s1;
  cin >> s1;
  reverse(s1.begin(), s1.end());
  long long m;
  cin >> m;
  deg[0] = 1;
  long long n = s1.size();
  for (int i = 1; i < s1.size(); i++) {
    deg[i] = (deg[i - 1] * 10) % m;
  }
  long long rev = 0;
  long long ans = 0;
  for (int i = n - 1; i >= 0; i--) {
    rev += (s1[i] - '0') * deg[i];
    rev %= m;
  }
  ans = rev;
  for (int i = n - 1; i >= 1; i--) {
    rev -= (s1[i] - '0') * deg[n - 1];
    rev %= m;
    if (rev < 0) rev += m;
    rev *= 10;
    rev %= m;
    rev += deg[0] * (s1[i] - '0');
    rev %= m;
    if (s1[i - 1] == '0') {
      continue;
    } else
      ans = min(rev, ans);
  }
  cout << ans << endl;
  return 0;
}
