#include <bits/stdc++.h>
using namespace std;
bool endline = false;
template <class T>
istream& operator>>(istream& inp, vector<T>& v) {
  for (auto& it : v) inp >> it;
  return inp;
}
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
  for (auto& it : v) out << it << (endline ? "\n" : " ");
  return out;
}
template <class T, class U>
istream& operator>>(istream& inp, pair<T, U>& v) {
  inp >> v.first >> v.second;
  return inp;
}
template <class T, class U>
ostream& operator<<(ostream& out, pair<T, U>& v) {
  out << v.first << ' ' << v.second;
  return out;
}
void debug() { cout << endl; }
template <typename H, typename... T>
void debug(H a, T... b) {
  cout << a << ' ';
  debug(b...);
}
void FAST_IO(string filein = "", string fileout = "") {
  if (fopen(filein.c_str(), "r")) {
    freopen(filein.c_str(), "r", stdin);
    freopen(fileout.c_str(), "w", stdout);
  }
  cin.tie(0), cout.tie(0)->sync_with_stdio(0);
}
void Hollwo_Pelw();
signed main() {
  FAST_IO(".inp", ".out");
  int testcases = 1;
  for (int test = 1; test <= testcases; test++) {
    Hollwo_Pelw();
  }
  return 0;
}
const int allmod[3] = {(int)1e9 + 7, 998244353, (int)1e9 + 9};
const int mod = allmod[1];
const int MAXN = 2e5 + 6;
const int inf = 2e9;
const long long linf = 1e18;
string s;
long long m, pf[MAXN];
long long power(long long a, long long x) {
  long long r = 1;
  while (x) {
    if (x & 1) r = r * a % m;
    x >>= 1;
    a = a * a % m;
  }
  return r;
}
long long calc(long long l, long long r) {
  return (pf[r] - pf[l - 1] * power(10, r - l + 1) % m + m) % m;
}
long long calc(long long id) {
  long long left = calc(1, id), right = calc(id + 1, s.size());
  return (right * power(10, id) % m + left % m) % m;
}
void Hollwo_Pelw() {
  cin >> s >> m;
  for (long long i = 1; i <= s.size(); i++)
    pf[i] = (pf[i - 1] * 10 + s[i - 1] - '0') % m;
  long long ans = pf[s.size()];
  for (long long i = 1; i < s.size(); i++) {
    if (s[i] == '0') continue;
    ans = min(calc(i), ans);
  }
  cout << ans;
}
