#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <bits/stdc++.h>
#include <chrono>
using namespace std;
#define forr(i,n) for(int i = 0 ; i  < n ; i++)
#define forr1(i,n) for(int i = 1 ; i  <= n ; i++)
#define pb push_back
// #define int long long
#define endl "\n"
#define ll long long
#define sz(a)  int(a.size())
const int mod  = 998244353;
const int N = 2e5 + 10 ;
const ll inf = 1e9;
const int maxm = 1005;
vector<vector<char>> matrix(maxm , vector<char>(maxm));
// vector<vector<bool>> vt(maxm, vector<bool>(maxm));
struct event {
  int x, y, size;
};
int dx[] = {0, 0, 1, - 1};
int dy[] = { -1, 1 , 0 , 0};
struct myComp {
  constexpr bool operator()(pair<int, int> const& a, pair<int, int> const& b)const noexcept
  {
    return a.first < b.first;
  }
};
bool cmp(pair<int, int>a , pair<int, int> b)
{
  if (a.first == b.first)
    return a.second < b.second ;
  return a.first < b.first ;
}

string s ;
pair<bool, int> chk(int j )
{
  int k = j ;
  int n = sz(s);
  for (int i = 0 ; i < n ; i++)
  {
    if ( k >= n)
    {
      // cout << "yes";
      if (s[1] < s[0]) return {false, j};
    }
    if (s[i] > s[k]) return {true, k};
    else if (s[i] < s[k]) return {false, k};
    else if (s[i] == s[k])k++;
    // cout << i << " " << k << " " << j << endl;
  }
  return {true, k} ;
}
void solve()
{
  int n ;
  cin >> n;
  map<int, int> m ;
  ll res = 0 ;
  forr(i, n)
  {
    int x ;
    cin >> x;
    res += m[x];
    m[x]++;
  }
  ll ans = (n * 1ll * (n - 1)) / 2 - res;
  cout << ans ;
}
int main()
{
#ifndef ONLINE_JUDGE
  freopen("input.txt" , "r" , stdin);
  freopen("3.txt" , "w" , stdout);
#endif
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  // int t;
  // cin >> t;
  // while (t--)
  solve();

}
#pragma region Region_1

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
using ll = long long;
using P = pair<int, int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;
using VP = vector<P>;
using VS = vector<string>;
using VC = vector<char>;
using VVC = vector<vector<char>>;
#define MOD 1000000007
const int INF = 1e9 + 10;          // int max > 2*10^9
const long long INFL = (long long)2e18 + 10;  // ll max > 9*10^18

template <class T, class C>
bool chmax(T& a, C b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T, class C>
bool chmin(T& a, C b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}

template <class T>
T sum(const vector<T>& v) {
    T res = 0;
    for (size_t i = 0; i < v.size(); ++i) res += v[i];
    return res;
}

/////////////////////////////////////////////////////////
// print like python
// https://qiita.com/Lily0727K/items/06cb1d6da8a436369eed
/////////////////////////////////////////////////////////
void print() { cout << endl; }

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
    cout << head;
    if (sizeof...(tail) != 0) cout << " ";
    print(forward<Tail>(tail)...);
}

template <class T>
void print(vector<T>& vec) {
    for (auto& a : vec) {
        cout << a;
        if (&a != &vec.back()) cout << " ";
    }
    cout << endl;
}

template <class T>
void print(vector<vector<T>>& df) {
    for (auto& vec : df) {
        print(vec);
    }
}

#pragma endregion Region_1
/////////////////////////////////////////////////////////

int main() {
    //入力の高速化用のコード
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    std::cout << std::setprecision(15);
    //////////////////////////////////////////
    string s;
    ll M;
    cin >> s;
    cin >> M;
    int maxd = 0;
    VI sv;
    for (char c : s) {
        chmax(maxd, (c - '0'));
        sv.push_back((c - '0'));
    }
    if(sv.size()==1){
        if(maxd <= M)print(1);
        else print(0);
        return 0;
    }

    auto n_fnc = [&](ll m) {
        if (m == 1) return false;
        ll v = 1;
        ll tot = 0;
        drep(i, sv.size()) {
            if (((ll)1e18+v-1) / v < sv[i]) return false;

            tot += v * sv[i];
            if (tot > M) {
                return false;
            }

            if (i > 0) {
                if (((ll)1e18+v-1) / v < m){
                     return false;
                }
            }
            v *= m;
        }
        return true;
    };
    ll l = maxd, r = (ll)1e18 + 1;
    while (l + 1 < r) {
        ll mid = (l + r) / 2;
        if (n_fnc(mid)) {
            l = mid;
        } else
            r = mid;
    }
    print(l-maxd);

    return 0;
}
