#include <bits/stdc++.h>
using namespace std;
char num[200001];
int numLen, m, temp, answer = 0;
int stepArr = 1;
int main() {
  cin >> num >> m;
  numLen = strlen(num);
  answer += (num[numLen - 1] - 48) % m;
  for (int i = numLen - 2; i >= 0; i--) {
    stepArr = (stepArr * 10) % m;
    answer += ((num[i] - 48) * stepArr) % m;
    answer %= m;
  }
  temp = answer;
  for (int i = 0; i < numLen - 1; i++) {
    temp = ((((temp - (num[i] - 48) * stepArr) % m + m) * 10) % m +
            (num[i] - 48) % m) %
           m;
    if (num[i + 1] ^ 48) {
      answer = min(answer, temp);
    }
  }
  cout << answer;
  return 0;
}
