/*

                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                    prayer
*/

// g++ -std=c++11 a.cpp
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<unordered_map>
#include<utility>
#include<cmath>
#include<random>
#include<cstring>
#include<queue>
#include<stack>
#include<bitset>
#include<cstdio>
#include<sstream>
#include<iomanip>
#include<assert.h>
#include<typeinfo>
#define loop(i,a,b) for(long long i=a;i<b;i++)
#define rep(i,a) loop(i,0,a)
#define FOR(i,a) for(auto i:a)
#define pb push_back
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)
#define show1d(v) {rep(_,v.size())cout<<" "<<v[_];cout<<endl;}
#define show2d(v) {rep(__,v.size())show1d(v[__]);}
using namespace std;
//kaewasuretyuui
typedef long long ll;
#define int ll
typedef int Def;
typedef pair<Def,Def> pii;
typedef vector<Def> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef pair<Def,pii> pip;
typedef vector<pip>vip;
#define mt make_tuple
typedef tuple<int,int,int,int> tp;
typedef vector<tp> vt;
template<typename A,typename B>bool cmin(A &a,const B &b){return a>b?(a=b,true):false;}
template<typename A,typename B>bool cmax(A &a,const B &b){return a<b?(a=b,true):false;}
const double PI=acos(-1);
const double EPS=1e-9;
Def inf = sizeof(Def) == sizeof(long long) ? 2e18 : 1e9+10;
#define yes cout<<"Yes\n"
#define no cout<<"No\n"

template< typename T >
istream &operator>>(istream &is, vector< T > &v) {
  for(T &in : v) is >> in;
  return is;
}

vs in;
vector<vs>d;
bool dfs(int a,int b){
    // cout<<a<<" "<<b<<endl;rep(i,4)cout<<in[i]<<endl;cout<<endl;
    if(a==b&&dfs(a+1,b))return true;
    if(a==4)return b==3;
    vs w=d[a];
    int n=w.size(),m=w[0].size();
    rep(i,5-n)rep(j,11-m){
        bool h=true;
        rep(x,n)rep(y,m)if(w[x][y]=='#'&&in[i+x][j+y]=='#')h=false;
        if(h){
            rep(x,n)rep(y,m)if(w[x][y]=='#')in[i+x][j+y]='#';
            if(dfs(a+1,b+1))return true;
            rep(x,n)rep(y,m)if(w[x][y]=='#')in[i+x][j+y]='.';
        }
    }
    return false;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);

    vi w(10);
    rep(i,10)w[i]=i;
    int m,MOD=1000000007;string s;
    cin>>m>>s;
    vi d(10);
    rep(i,s.size()){
        rep(j,10)(d[j]*=10)%=MOD;
        d[s[i]-'0']++;
    }
    do{
        if(w[s[0]-'0']||s.size()==1){
            int c=0;
            rep(i,10)(c+=d[i]*w[i]%MOD)%=MOD;
            if(c==m){
                rep(i,s.size())cout<<w[s[i]-'0'];cout<<endl;
                return 0;
            }
        }
    }while(next_permutation(all(w)));
    cout<<-1<<endl;
}



Measured memory_bytes: 3352