/*
   Author   : MatsuTaku
   Date     : 02/20/21
*/

#include <bits/stdc++.h>
using namespace std;
#include <x86intrin.h>
#ifndef ONLINE_JUDGE
#define Assert(cond)      assert(cond)
#else
#define Assert(cond)
#endif
#define rep(i, n)         for (size_t i = 0, i##_len = (n); i < i##_len; i++)
using lint = long long int;
using ulint = unsigned long long int;
template<typename T>
using vvec = vector<vector<T>>;
template<typename T>
vvec<T> make_vvec(int n, int m, T v) { return vvec<T>(n, vector<T>(m, v)); }
template<typename T>
void chmax(T& dst, T x) { dst = max(dst, x); }
template<typename T>
void chmin(T& dst, T x) { dst = min(dst, x); }

class Solver {
 public:
  Solver();
  void solve();
};

Solver::Solver() {}

void Solver::solve() {
  string x; cin>>x;
  ulint m; cin>>m;
  ulint d = *max_element(x.begin(), x.end())-'0' + 1;
  auto check = [&](ulint _d) {
    ulint k = 0;
    rep(i, x.size()) {
      ulint v = x[i]-'0';
      if (k > (m - v) / _d)
        return false;
      k = k * _d + v;
      if (k > m)
        return false;
    }
    return k <= m;
  };

  if (x.size() == 1) {
    int ans = check(d);
    cout << ans << endl;
    return;
  }
  if (check(d)) {
    ulint l = d, r = 11e17;
    while (l+1 < r) {
      auto c = (l+r)/2;
      if (check(c)) {
        l = c;
      } else {
        r = c;
      }
    }
    cout << l-d+1 << endl;
  } else {
    cout << 0 << endl;
  }
}

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  cout<<fixed<<setprecision(10);
  Solver solver;
  int t = 1;
//  cin>>t;
  while (t--) {
    solver.solve();
  }
  return 0;
}

#include <bits/stdc++.h>
typedef long long ll;
typedef std::vector<int> vi;
typedef std::vector<std::vector<int>> vii;
typedef std::pair<int, int> pii;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
#define rep(i, n) for(int i = 0; i < (int)(n); ++i)
#define repi(i, a, b) for(int i = a; i < (int)(b); ++i)
#define all(x) (x).begin(),(x).end()
#define SZ(x) ((int)(x).size())
#define inf 2147483647
#define inf64 9223372036854775807
#define V vector
#define Q queue
#define PQ priority_queue
#define pb push_back
#define mp make_pair
#define F first
#define S second
using namespace std;


void solution()
{
    int n;
    cin >> n;

    V<ll> v;

    ll na = 0;
    rep(i, n) {
        ll a, b;
        cin >> a >> b;
        na += a;

        v.pb(a + a + b);
    }

    sort(all(v), greater<ll>());

    ll ans = 0;
    for(auto val : v) {
        na -= val;
        ans++;

        if(na < 0){
            break;
        }
    }

    cout << ans << endl;

}

int main()
{
    std::cin.tie(nullptr);
    std::ios_base::sync_with_stdio(false);
    std::cout << std::fixed << std::setprecision(15);
    solution();
    return 0;
}
