#include<bits/stdc++.h>
#define ll long long
#define rep(i,x,y) for(int i=x,i##end=y;i<=i##end;++i)
#define _rep(i,x,y) for(int i=x,i##end=y;i>=i##end;--i)
using pii=std::pair<int,int>;
using u32=unsigned;
using u64=unsigned ll;
inline bool up(int&x,const int y){return x<y?x=y,1:0;}
inline bool down(int&x,const int y){return x>y?x=y,1:0;}
#define N 1005
using vec=int[N];
vec dp,a,b,cpy;
vec l,r;
#define FAIL std::cout<<"No\n"
int main(){
	std::ios::sync_with_stdio(0);
	int n,cnt=0; std::cin>>n;
	rep(i,1,n){
		int x,y; std::cin>>x>>y;
		if(x>0&&y>0&&x>y){
			return FAIL,0;
		}
		if(x>0) l[x]=1,a[x]=std::max(y,0);
		if(y>0) r[y]=1,b[y]=std::max(x,0);
		cpy[++cnt]=x,cpy[++cnt]=y;
	}
	std::sort(cpy+1,cpy+cnt+1);
	rep(i,2,cnt){
		if(cpy[i]==cpy[i-1]&&cpy[i]>0&&cpy[i-1]>0){
			return FAIL,0;
		}
	}
	dp[0]=1;
	rep(i,1,n) rep(j,0,i-1) if(dp[j]){
		bool ty=1;
		rep(k,j+1,i){
			if((a[j+k]&&a[j+k]!=i+k)||l[i+k]||(b[i+k]&&b[i+k]!=j+k)||r[j+k])
				ty=0;
			if(l[j+k]&&r[i+k]&&a[j+k]==0&&b[i+k]==0) ty=0;
		}
		dp[i]|=ty;
	}
	std::cout<<(dp[n]?"Yes":"No")<<'\n';
	return 0;
}
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <set>
#include <vector>
#include <queue>
#include <map>
#include <numeric>
#include <math.h>
#include <complex>

using namespace std;

#define rep(i, n) for (long long int i = 0; i < (long long int)(n); i++)
#define irep(i, n) for (long long int i = 1; i <= (long long int)(n); i++)
#define drep(i, n, diff) for (long long int i = 0; i < (long long int)(n); i += diff)
#define mod 1000000007
#define INF 1001001001001001011
#define ld(val) printf("%s : %lld\n", #val, val);
#define sd(val) printf("%s : %s\n", #val, val.c_str());

typedef long long int ll;
typedef pair<ll, ll> P;
typedef pair<P, P> PP;
typedef tuple<ll, ll, ll> TP;

int main() {
  string x;
  ll m;
  cin >> x >> m;
  ll md = 0;
  rep(i, x.size()) {
    ll t = x[i]-'0';
    md = max(md, t);
  }

  if(x.length() == 1) {
    if(x[0]-'0'<=m) cout << 1 << endl;
    else cout << 0 << endl;
    return 0;
  }

  ll l=md;
  ll r=m+1;
  while(r-l>1) {
    ll v = (l+r)/2;
    ll ret = 0;
    for(ll i=0; i<x.size(); i++) {
      ll t = x[i] - '0';
      if (ret > m/v) {
        ret = m+1;
        break;
      } else {
        ret = (ret*v) + t;
      }
    }
    if(ret>m) {
      r=v;
    } else {
      l=v;
    }
  }
  cout << l-md << endl;
  return 0;
}
