#include <bits/stdc++.h>
int dis[410000], i, j, n, m, k, l, len, mo, ans, now;
char s[410000];
int main() {
  scanf("%s", s);
  scanf("%d", &mo);
  for (i = 0;; i++)
    if (s[i] >= 48 && s[i] <= 58)
      dis[++len] = s[i] - 48;
    else
      break;
  int x = 1, y = 1;
  for (i = len; i > 1; --i) {
    x = x * 10 % mo;
  }
  for (i = len; i >= 1; i--) {
    now = (now + dis[i] * y) % mo;
    y = (y * 10) % mo;
  }
  ans = now;
  for (i = 1; i <= len; i++) {
    now = ((now - (dis[i] * x) % mo) + mo) % mo;
    now = (now * 10) % mo;
    now = (now + dis[i]) % mo;
    if (dis[i + 1] != 0 && now < ans) ans = now;
  }
  printf("%d", ans);
}
