stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



#define rep(i,n) for(i = 0;i < n;++i)

#define all(v) v.begin(), v.end()

using ll = long long;



int main()

{

    ll i,j;

    ll n,x,m;

    cin >> n >> x >> m;

    vector<vector<int>> dub(m, vector<int>(40, -1));

    vector<vector<ll>> dubsum(m, vector<ll>(40, 0));

    rep(i, m){

        dub[i][0] = i*i%m;

    }

    rep(i, 39){

        rep(j,m){

            dub[j][i + 1] = dub[dub[j][i]][i];

        }

    }

    rep(i,m){

        dubsum[i][0] = i;

    }

    rep(i,39){

        rep(j,m){

            dubsum[j][i+1] = dubsum[j][i] + dubsum[dub[j][i]][i];

        }

    }

    ll ans = 0;

    int bit = 0;

    int now = x;

    while(n){

        if(n & 1){

            ans += dubsum[now][bit];

            now = dub[now][bit];

        }

        ++bit;

        n >>= 1;

    }

    cout << ans << endl;



    return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;



#define rep(i,n) for(i = 0;i < n;++i)

#define all(v) v.begin(), v.end()

using ll = long long;



int main()

{

    ll i,j;

    ll n,x,m;

    cin >> n >> x >> m;

    vector<ll> spare(m, -1);

    ll now = x;

    /**

    if(n <= 1e7){

        ll ans = 0;

        ll now = x;

        rep(i,n){

            ans += now;

            now = now*now%m;

        }

        cout << ans << endl;

        return 0;

    }**/

    rep(i,m){

        if(spare[now] != -1) break;

        else{

            spare[now] = i;

            now = now*now%m; 

        }

    }

    ll start = spare[now];

    ll end = i;

    ll shuuki = end-start;

    now = x;

    ll ans = 0;

    for(i = 0;i < start;++i){

        ans += now;

        now = now*now%m;

        --n;

        if(n == 0){

            cout << ans << endl;

            return 0;

        }

    }

    ll kurikaesi = 0;

    for(i = start;i < end;++i){

        kurikaesi += now;

        now = now*now % m;

    }

    ans += n/shuuki*kurikaesi;

    n %= shuuki;

    while(n){

        ans += now;

        now = now*now%m;

        --n;

    }

    cout << ans << endl;



    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

// #include <atcoder/all>

using namespace std;

// using namespace atcoder;

using ll = long long;

using ull = unsigned long long;

#define REP(i,n) for (ll i = 0; i < (n); ++i)

#define RREP(i, n) for (ll i = (n) - 1; i >= 0; --i)

#define ALL(v) (v).begin(), (v).end()

template<class T>

using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;





int main(){

    ll n, x, m;

    cin >> n >> x >> m;



    vector<vector<ll>> nxt(40, vector<ll>(m, 0));

    vector<vector<ll>> s(40, vector<ll>(m, 0));

    REP(y, m){

        nxt.at(0).at(y) = (y * y) % m;

        s.at(0).at(y) = y;

    }



    for(ll i = 1; i < 40; ++i){

        REP(y, m){

            const ll n1 = nxt.at(i - 1).at(y);

            s.at(i).at(y) = s.at(i - 1).at(n1) + s.at(i - 1).at(y);

            nxt.at(i).at(y) = nxt.at(i - 1).at(n1);

        }

    }



    ll ans = 0;

    for(ll i = 0; i < 40; ++i){

        if((n >> i) & 1){

            ans += s.at(i).at(x);

            x = nxt.at(i).at(x);

        }

    }

    cout << ans << endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

// #include <atcoder/all>

using namespace std;

// using namespace atcoder;

using ll = long long;

using ull = unsigned long long;

#define REP(i,n) for (ll i = 0; i < (n); ++i)

#define RREP(i, n) for (ll i = (n) - 1; i >= 0; --i)

#define ALL(v) (v).begin(), (v).end()

template<class T>

using reverse_priority_queue = priority_queue<T, vector<T>, greater<T>>;





template<typename S, S op(S, S)>

struct Doubling {

    vector<vector<S>> v;



    Doubling(const vector<S>& step1, ll p){

        const auto n = step1.size();

        v.resize(p);

        v.at(0) = step1;

        for(ll i = 1; i < p; ++i){

            v.at(i).resize(n);

            for(ll j = 0; j < n; ++j){

                const auto& x = v.at(i - 1).at(j);

                const auto& y = v.at(i - 1).at(x.index);

                v.at(i).at(j) = op(x, y);

            }

        }

    }



    S query(S init, ll nstep){

        S ret = init;

        REP(i, v.size()){

            if((nstep >> i) & 1){

                ret = op(ret, v.at(i).at(ret.index));

            }

        }

        return ret;

    }

};



struct S{ ll sum, index; };

S op(S x, S y){ return S{x.sum + y.sum, y.index }; }



int main(){

    ll n, x, m;

    cin >> n >> x >> m;



    vector<S> step1(m);

    REP(i, m){

        step1.at(i) = {i, (i * i) % m};

    }



    Doubling<S, op> dbl(step1, 40);



    const auto ans = dbl.query(S{0, x}, n);

    cout << ans.sum << endl;



    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define P pair<ll,ll>

#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)

#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)

#define TO(x,t,f) ((x)?(t):(f))

#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9

#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v  x is sorted

#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v  x is sorted

#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted

#define REV(x) (reverse(x.begin(),x.end())) //reverse

ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);}

ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);}

#define NEXTP(x) next_permutation(x.begin(),x.end())

const ll INF=ll(1e16)+ll(7);

const ll MOD=1000000007LL;

#define out(a) cout<<fixed<<setprecision((a))

//tie(a,b,c) = make_tuple(10,9,87);

#define pop_(a) __builtin_popcount((a))

ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;}







int main(){

	ll N,X,M;

	cin >> N >> X >> M;



	//今iにいて2^j個先に行ったとき



	ll pla[M][35]={},bal[M][35]={};



	FOR(i,0,M){

		pla[i][0] = i*i % M;

		bal[i][0] = i*i % M;

	}

	FOR(j,1,35)FOR(i,0,M){

		pla[i][j] = pla[ pla[i][j-1] ][j-1];

		bal[i][j] = bal[i][j-1] + bal[ pla[i][j-1] ][j-1];

	}



	ll ans = X;

	N--;

	FOR(i,0,35){

		if(N&(1LL<<i)){

			ans += bal[X][i];

			X = pla[X][i];

		}

	}

	cout << ans << endl;



}


// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define P pair<ll,ll>

#define FOR(I,A,B) for(ll I = ll(A); I < ll(B); ++I)

#define FORR(I,A,B) for(ll I = ll((B)-1); I >= ll(A); --I)

#define TO(x,t,f) ((x)?(t):(f))

#define SORT(x) (sort(x.begin(),x.end())) // 0 2 2 3 4 5 8 9

#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin()) //xi>=v  x is sorted

#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin()) //xi>v  x is sorted

#define NUM(x,v) (POSU(x,v)-POSL(x,v))  //x is sorted

#define REV(x) (reverse(x.begin(),x.end())) //reverse

ll gcd_(ll a,ll b){if(a%b==0)return b;return gcd_(b,a%b);}

ll lcm_(ll a,ll b){ll c=gcd_(a,b);return ((a/c)*b);}

#define NEXTP(x) next_permutation(x.begin(),x.end())

const ll INF=ll(1e16)+ll(7);

const ll MOD=1000000007LL;

#define out(a) cout<<fixed<<setprecision((a))

//tie(a,b,c) = make_tuple(10,9,87);

#define pop_(a) __builtin_popcount((a))

ll keta(ll a){ll r=0;while(a){a/=10;r++;}return r;}







int main(){

	ll N,X,M;

	cin >> N >> X >> M;

	ll ans = 0;



	vector<ll> did(M+9,0),sum(M+9,0);

	ll b = M;



	while(N>0){

		if(did[X]){

			ll loop_point = sum[b] - sum[X] + X;

			if(b==X){

				ans += X*N;

				cout << ans << endl;

				return 0;

			}

			ans += loop_point * ( N / (did[b]-did[X]+1) );

			N %= (did[b]-did[X]+1);

		}



		if(N>0){

			did[X] = did[b] + 1;

			N--;

			ans += X;

			sum[X] = sum[b] + X;

			b = X;

			X = X*X%M;

		}



		if(X==0)break;



	}





	cout << ans << endl;



}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

#define ALL(x) x.begin(),x.end()

#define rep(i,n) for(int i=0;i<n;i++)

#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; 

#define INF 1000000000

#define mod 1000000007

typedef long long ll;

const ll LINF = 1001002003004005006ll;

int dx[]={1,0,-1,0};

int dy[]={0,1,0,-1};

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

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; }



signed main(){

    cin.tie(0);

    ios::sync_with_stdio(0);



    int n,k;cin>>n>>k;

    vector<ll> h(n+1);

    rep(i,n) cin>>h[i+1];



    //まず，ある列を操作すること，その列を完全に消すことは同値(これは自分で気づけた)

    //消す列を選ぶ

    //うまくDPするために

    //必要な情報はDPのindex(次元？)に持たせる

    //何回操作したかとか，あと何回操作したかとかをDPに持たせるのはとても自然な考えだけど，もう一つ

    //今回，ある状況での部分解は削除しなかった最後の列に依るんだからそこを持ったほうがいいとわかる

    //なるほどなぁ

    ll dp[310][310];

    rep(i,310)rep(j,310) dp[i][j]=LINF;

    dp[0][0]=0;

    // dp[i][j] : 最後にi列を使った，サイズがj

    // dp[i][j]=min(k=1 to i-1  dp[k][j-1]+max(0,h[i]-h[k]))



    for(int j=1;j<=n-k;j++){

        for(int i=1;i<=n;i++){

            for(int k=0;k<i;k++){

                chmin(dp[i][j],dp[k][j-1]+max(0ll,h[i]-h[k]));

            }

        }

    }

    ll ans=LINF;

    rep(i,n+1){

        chmin(ans,dp[i][n-k]);

    }

    cout<<ans<<endl;

    return 0;

}


// Optimized solution to the same problem (faster):
#include<bits/stdc++.h>

using namespace std;

#define ALL(x) x.begin(),x.end()

#define rep(i,n) for(int i=0;i<n;i++)

#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; 

#define INF 1000000000

#define mod 1000000007

using ll=long long;

const ll LINF = 1001002003004005006ll;

int dx[]={1,0,-1,0};

int dy[]={0,1,0,-1};

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}

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; }



int n,k;

vector<ll> h;



inline ll cost(int a,int b){

    return (max(0ll,h[b]-h[a]));

}



signed main(){

    cin.tie(0);

    ios::sync_with_stdio(0);

    

    cin>>n>>k;

    h.resize(n+1);

    rep(i,n) cin>>h[i+1];

    h[0]=0;



    if(k>=n){

        cout<<0<<endl;

        return 0;

    }



    ll dp[n+1][k+1];

    rep(i,n+1){

        rep(j,k+1){

            dp[i][j]=INF*100ll;

        }

    }

    dp[0][0]=0;



    rep(j,k+1){

        rep(i,n){

            for(int l=0;l<=k and j-l>=0 and i-l>=0;l++){

                chmin(dp[i+1][j],dp[i-l][j-l]+cost(i-l,i+1));

            }

        }

    }

    // rep(j,k+1){

    //     rep(i,n+1){

    //         cout<<dp[i][j]<<"  ";

    //     }cout<<endl;

    // }

    ll ans=LINF;

    rep(i,k+1){

        chmin(ans,dp[n-i][k-i]);

    }

    cout<<ans<<endl;

    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

#define repr(i, a, b) for (int i = a; i < b; i++)

#define rep(i, n) for (int i = 0; i < n; i++)

#define invrepr(i, a, b) for (int i = b - 1; i >= a; i--)

#define invrep(i, n) invrepr(i, 0, n)

const int MOD = 1e9 + 7;



int main()

{

    int n, k;

    cin >> n >> k;

    vector<int> use;

    vector<int> d(k);

    rep(i, k) cin >> d[i];

    sort(d.begin(), d.end());

    int m = 0;

    rep(i, 10)

    {

        if (i != d[m])

            use.push_back(i);

        else

            ++m;

    }

    int ans;

    repr(i,n,100000) {

        int j = i;

        vector<int> value;

        while(j>0) {

            value.push_back(j%10);

            j/=10;

        }

        bool flag = 1;

        rep(j,value.size()) {

            bool flag1 = 0;

            rep(l,use.size()) {

                if (value[j]==use[l]) flag1=1;

            }

            if (!flag1) flag=0;

        }

        if (flag) {

            ans=i;

            break;

        }

    } 

    cout << ans << endl;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;

#define repr(i,a,b) for(int i=a;i<b;i++)

#define rep(i,n) for(int i=0;i<n;i++)

#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)

#define invrep(i,n) invrepr(i,0,n)

#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)

#define P pair<int,int>

const int MOD=998244353;

const ll INF=2e15;

const double PI=acos(-1);





int main() {

    ios_base::sync_with_stdio(false); 



    ll n,k;

    cin >> n >> k;

    vector<int> d(k);

    rep(i,k) cin >> d[i];

    sort(d.begin(),d.end());

    int ans=0;

    repr(i,n,n+1000000) {

        bool flag=1; 

        int j=i;

        while (j>0) {

            int l=j%10;

            bool okay=1;

            rep(m,k) {

                if (l==d[m]) okay=0;

            }

            if (!okay) {

                flag=0;

                break;

            }

            j/=10;

        }

        if (flag) {

            ans=i;

            break;

        }

    }

    cout << ans << endl;





    return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <unordered_set>

#include <string>



#include <algorithm>



using namespace std;



using us = unordered_set<int>;



int main() {

	int n, k;

	cin >> n >> k;

	us availables = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};



	for(int i = 0; i < k; i++) {

		int num; cin >> num;

		availables.erase(num);

	}



	while(true) {

		string num = to_string(n);

		bool found = false;

		for(int i = 0; i < num.size(); i++) {

			if(availables.find(num[i] - '0') == availables.end()) {

				break;

			}

			if(i == num.size() - 1) {

				found = true;

			}

		}

		if(found) {

			break;

		} else {

			n++;

		}

	}

	cout << n << endl;

}

// Optimized solution to the same problem (faster):
#include <algorithm>

#include <bitset>

#include <cmath>

#include <iostream>

#include <limits>

#include <list>

#include <map>

#include <queue>

#include <set>

#include <stack>

#include <string>

#include <unordered_map>

#include <unordered_set>

#include <utility>

#include <vector>



using namespace std;

using ll = long long;

const int IINF = 1e9;

const ll INF = 1e18;

const int MOD = 1e9 + 7;



#define dump(x) cout << #x << " = " << (x) << endl;

#define YES(n) cout << ((n) ? "YES" : "NO") << endl



template <typename T>

void dumpvec(vector<T> vec) {

  for (T cnt : vec) {

    cout << cnt << ", ";

  }

  cout << endl;

}



void chmin(ll &a, ll b) {

  if (a > b) {

    a = b;

  }

}

void chmax(ll &a, ll b) {

  if (a < b) {

    a = b;

  }

}



vector<string> split(string s, string delimiter) {

  int first = 0;

  int last = s.find_first_of(delimiter);

  vector<string> result;



  while (first < s.size()) {

    string sub = s.substr(first, last - first);

    result.push_back(sub);

    first = last + 1;

    last = s.find_first_of(delimiter, first);



    if (last == string::npos) {

      last = s.size();

    }

  }



  return result;

}



string join(vector<string> strings, string delimiter) {

  string result = "";

  for (int i = 0; i < strings.size(); i++) {

    if (result != "") result += delimiter;

    result += strings[i];

  }

  return result;

}



class Solution {

 public:

  void solve() {

    int N, K;

    cin >> N >> K;

    set<int> availables{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    for (int i = 0; i < K; i++) {

      int n;

      cin >> n;

      availables.erase(n);

    }

    while (true) {

      // dump(N);

      if (satisfy(N, availables)) {

        cout << N << endl;

        return;

      }

      N++;

    }

  }



  bool satisfy(int n, set<int> &availables) {

    if (n == 0) {

      return availables.count(0) > 0;

    }



    while (n > 0) {

      int digit = n % 10;

      if (availables.count(digit) == 0) return false;

      n /= 10;

    }

    return true;

  }

};



int main() { Solution().solve(); }

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <algorithm>

#include <iostream>

#include <set>

#include <vector>



namespace {



typedef std::vector<int> IntVector;



/**

 * return degit length

 * It will return 1 if val is 0

 */

int degit_length(int val) {

  int length = 1;

  val /= 10;

  while (val) {

    length += 1;

    val /= 10;

  }



  return length;

}



int pow10(int n) {

  int res = 1;

  for (int i = 0; i < n; ++i) {

    res *= 10;

  }



  return res;

}



IntVector slice_degit(int val) {

  IntVector iv;



  iv.push_back(val % 10);

  for (int i = 1; i < degit_length(val); ++i) {

    iv.push_back((val / pow10(i)) % 10);

  }



  return iv;

}



} // namespace



int main() {

  std::cin.tie(nullptr);

  std::ios::sync_with_stdio(false);



  int n, k;



  std::cin >> n >> k;



  int ki[10];

  for (int i = 0; i < 10; ++i) {

    ki[i] = 0;

  }



  int t;

  for (int i = 0; i < k; ++i) {

    std::cin >> t;

    ki[t] = 1;

  }



  int res = n;

  for (;;) {

    IntVector iv = slice_degit(res);

    bool is_ng = false;

    for (auto v : iv) {

      if (ki[v]) {

        is_ng = true;

        break;

      }

    }



    if (!is_ng) {

      break;

    }

    ++res;

  }



  std::cout << res << std::endl;



  return 0;

}


// Optimized solution to the same problem (faster):
#include <algorithm>

#include <iostream>

#include <set>

#include <vector>



int main() {

  std::cin.tie(nullptr);

  std::ios::sync_with_stdio(false);



  int n, k;



  std::cin >> n >> k;



  int ki[10];

  for (int i = 0; i < 10; ++i) {

    ki[i] = 0;

  }



  int t;

  for (int i = 0; i < k; ++i) {

    std::cin >> t;

    ki[t] = 1;

  }



  int res = n;

  for (;;) {

    if (ki[res % 10]) {

      ++res;

      continue;

    }



    int val = res / 10;

    bool is_ng = false;

    while (val != 0) {

      if (ki[val % 10]) {

        ++res;

        is_ng = true;

        break;

      }

      val /= 10;

    }

    if (!is_ng) {

      break;

    }

  }



  std::cout << res << std::endl;



  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <vector>

#include <cmath>

#include <string>

#include <algorithm>

using namespace std;

int main() {

  int n, k;

  vector<char> v;

  cin >> n >> k;

  while(k--) {

    char a;

    cin >> a;

    v.push_back(a);

  }

  int c = n;

  while (1) {

    bool bad = false;

    for (char d : to_string(c)) {

      for (char dc : v) {

        if (d == dc) {

          bad = true;

          c++;

          break;

        }

      }

      if (bad) {

        break;

      }

    }

    if (!bad) {

      break;

    }

  }

  cout << c << endl;

  return 0;

}

  


// Optimized solution to the same problem (faster):
#include <iostream>

#include <vector>

#include <cmath>

#include <string>

#include <algorithm>

#include <numeric>

#include <climits>

#define LIM 1000007

#define ll long long

using namespace std;

bool v[10];

bool badnum(ll n) {

  int k = n;

  while (k != 0) {

    if (v[k % 10]) {

      return true;

    }

    k /= 10;

  }

  return false;

}

int main() {

  ll n, k;

  cin >> n >> k;

  for (int i = 0; i < k; i++) {

    int a;

    cin >> a;

    v[a] = true;

  }

  while (badnum(n)) {

    n++;

  }

  cout << n << endl;

  return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <string>

#include <vector>

#include <algorithm>



using namespace std;



int main() {

    int N, K, D;

    int like[10] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};





    cin >> N >> K;



    for(int i = 0;i < K; ++i) { // 1 is considered true

        cin >> D;

        like[D] = 0; // dislike a number

    }



    int flag = false;

    for(int i = N;i < 100000; ++i) {

        string sN = to_string(i);

        for(int j = 0;j < sN.size(); ++j) { // per number like/not

            int indx = int(sN[j] - 48);

            if(like[indx] == 0) {

                break;

            } else if(j == sN.size() - 1) {

                flag = true;

            }

        }

        if(flag) {

            cout << i;

            break;

        }

    }



}


// Optimized solution to the same problem (faster):
#include <iostream>

#include <string>

#include <vector>

#include <algorithm>



using namespace std;



int like[10] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};



int check(int x) {

    int y;

    while(x) {

        int y = x % 10;

        if(like[y] == false) {

            return false;

        }

        x /= 10;

    }

    return true;

}



int main() {

    int N, K, D;





    cin >> N >> K;



    for(int i = 0;i < K; ++i) { // 1 is considered true

        cin >> D;

        like[D] = 0; // dislike a number

    }



    int x, y;

    for(int i = N;i < 100000; ++i) {

        if(check(i)) {

            cout << i;

            break;

        }



    }



}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;

int main() {

	int n, k;

	scanf ( "%d %d", &n, &k );

	int d[k];

	for ( int i = 0; i < k; i++ )

		scanf ( "%d", &d[i] );

	bool used[10];

	for ( int i = 0; i < 10; i++ )

		used[i] = true;

	for ( int i = 0; i < k; i++ )

		used[d[i]] = false;

	for ( int i = n; ; i++ ) {

		string s = to_string ( i );

		bool ok = true;

		for ( int j = 0; j < s.size(); j++ ) 

			if ( !used[s[j] - '0'] ) 

				ok = false;

		if ( ok ) {

			printf ( "%d\n", i );

			break;

		}

	}

	return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<vector>

using namespace std;

int k,N;

bool vis[20];

bool Pan(int x)

{

	while(x>0) 

	{

		if(vis[x%10])

			return 0;

		x/=10;

	}

	return 1;

}

int main()

{

	scanf("%d%d",&N,&k);

	for(int i=1;i<=k;i++)

	{

		int x;

		scanf("%d",&x);

		vis[x]=1;

	}

	if(N==0&&vis[0])

	{

		printf("1");

		return 0;

	}

	for(int i=N;;i++)

		if(Pan(i))

		{

			printf("%d",i);

			return 0;

		}

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#define _CRT_SECURE_NO_WARNINGS

#pragma _attribute_((optimize("-O2")))



#include <iostream>

#include <queue>

#include <stack>

#include <cstdio>

#include <vector>

#include <map>

#include <list>

#include <set>

#include <cstdio>

#include <bitset>

#include <algorithm>

#include <cmath>

#include <cstring>

#include <cstdlib>

#include <string>

#include <sstream>

#include <time.h>

#include <iomanip>



//#define Local



using namespace std;



set<int> dis;



bool chs(int in)

{

	bool flag = 1;

	while(flag&&in>0)

	{

		int tmp = in % 10;

		in /= 10;

		if (dis.count(tmp) != 0)

			flag = 0;

	}

	return flag;

}



int main()



{



	ios::sync_with_stdio(false);

	cin.tie(0);

	//freopen("input1.txt", "r", stdin);

	//freopen("output1.txt", "w", stdout);



	int t, k;

	while(cin>>t>>k)

	{

		dis.clear();

		while(k--)

		{

			int tmp;

			cin >> tmp;

			dis.insert(tmp);

		}

		while(1)

		{

			if(chs(t))

			{

				cout << t << endl;

				break;

			}

			t++;

		}

	}



#ifdef Local

	cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;

#endif

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<algorithm>

#include<vector>

using namespace std;

int k,N;

bool vis[20];

bool Pan(int x)

{

	while(x>0) 

	{

		if(vis[x%10])

			return 0;

		x/=10;

	}

	return 1;

}

int main()

{

	scanf("%d%d",&N,&k);

	for(int i=1;i<=k;i++)

	{

		int x;

		scanf("%d",&x);

		vis[x]=1;

	}

	if(N==0&&vis[0])

	{

		printf("1");

		return 0;

	}

	for(int i=N;;i++)

		if(Pan(i))

		{

			printf("%d",i);

			return 0;

		}

}

// Fastest known solution for this problem: