#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll INF = 100000000;
#define rep1(i, n) for (ll i=0; i<(n); i++) 
#define rep2(i, k, n) for (ll i=k; i<(n); i++)
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }


int main() {
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n, -1), s(n), c(n);
    rep1(i, m) cin >> s[i] >> c[i];
    rep1(i, m){
        if (a[s[i]-1]!=-1 && a[s[i]-1]!=c[i]){
            cout << -1 << endl;
            return 0;
        }
        a[s[i]-1] = c[i];
    }
    if (a[0]==0 && n>1){
        cout << -1 << endl;
        return 0;
    }
    rep1(i, n){
        if (i==0 && n>1){
            if (a[i]==-1) cout << 1;
            else cout << a[i];
        }else{
            if (a[i]==-1) cout << 0;
            else cout << a[i];
        }
    }
}
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int main(int argc, char **argv)
{
	string n;
  ll k;
  cin>>n;
  cin>>k;
  set <ll> arr;
  for(ll i=0;i<k;i++){
    ll x;
    cin>>x;
    arr.insert(x);
  }
  
  // checking for the case when the n is fine
  ll flag=1;
  for(ll i=0;i<n.size();i++){
    ll temp=n[i]-'0';
    if (arr.find(temp)!=arr.end()){
      flag=0;
      break;
    }
  }
  
  if (flag) {
    cout<<n<<"\n";
    return 0;
  }
  
  ll num=stoi(n);
  //cout<<"pika"<<"\n";
  while(num){
    ll ok=1;
    ll temp=num;
    while(temp!=0){
      ll digit=temp%10;
      temp=temp/10;
      //cout<<digit<<" "<<temp<<"\n";
      if (arr.find(digit)!=arr.end()){
        ok=0;
        break;
      }
    }
    if (ok){
      cout<<num<<"\n";
      return 0;
    }
    num++;
  }
  
	return 0;
}
