#include <bits/stdc++.h>

using namespace std;

int main(void){

    long long int N;
    int M,T;
  
    cin >> N >> M;

    int an,bn;

    int rn[M+1][M+1];

    for (int i=0;i<M+1;i++){
        for (int j=0;j<M+1;j++){
            rn[i][j] = -1;
        }
    }

    if (M!=1){
    an = 1;
    bn = 0;
    }else{
    an = 0;
    bn = 1;
    }

    for (int i=1;i<M*M+10;i++){ 

    bn = (10*bn+(10*an)/M)%M;
    an = (10*an)%M;

    if (rn[an][bn] == -1){
        rn[an][bn] = i;
    }else{
        T=i-rn[an][bn];
        N = min(N,(N-rn[an][bn])%T+rn[an][bn]);
        break;
    }

    }
    for (int i=0;i<M+1;i++){
        for (int j=0;j<M+1;j++){
        if (rn[i][j] == N){
        cout << j << endl;
        return 0;
        }
        }
    }
    

    

}
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, s, n) for (int i = (s); i < (n); ++i)
#define rrep(i, n) for (int i = n - 1; i >= 0; --i)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define len(x) (int)(x).size()
#define pb push_back
#define em emplace_back
#define vi vector<int>
#define vl vector<long long>
#define vs vector<string>
#define vc vector<char>
#define vd vector<double>
#define vb vector<bool>
#define vvi vector<vector<int>>
#define vvl vector<vector<long long>>
#define fast  \
  cin.tie(0); \
  ios::sync_with_stdio(false);
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
  fast;
  ll n, m;
  cin >> n >> m;
  ll l = m * m;
  ll cnt = 10, ans = 1;
  while (n > 0) {
    if (n % 2 == 1) {
      ans *= cnt;
      ans %= l;
    }
    cnt = cnt * cnt;
    cnt %= l;
    n /= 2;
  }
  cout << ans / m << endl;
  return 0;
}