/************************************
 *  Event  :      Programming olympad, Vologda 2008
 *  Problem:      Number
 *  Author:       Pavel Kuznetsov
 *  Last update:  2008.02.20 11:19
 ************************************
*/

#include <map> 
#include <set> 
#include <queue> 
#include <bitset> 
#include <valarray> 
#include <complex> 
#include <iostream> 
#include <sstream> 
#include <cmath> 
#include <memory.h>
#include <algorithm> 
#include <string> 
#include <cassert> 

using namespace std;

// prewritten code

#define Size(x) (int)(x).size()
#define For(i,a,b) for(int i=(int)(a);i<=(int)(b);++i)
#define Ford(i,a,b) for(int i=(int)(a);i>=(int)(b);--i) 
#define RepV(i,v) for (int i=0;i<Size(v);++i)
#define All(c) (c).begin(),(c).end()
#define Fill(a,b) memset(&a,b,sizeof(a))
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define Abs(a) ((a)<0?-(a):(a))
#define VVI vector<vector<int> >
#define VI vector<int>
#define VVS vector<vector<string> >
#define VS vector<string>
#define ForEach(it,a) for (typeof((a).begin()) it=(a).begin(); it!=(a).end(); ++it)
#define DBG(x) cout << #x <<" = "<< x << endl;
#define DBGA(x) {cout << #x <<": "; for (int i=0; i<(int)sizeof(x)/(int)sizeof(x[0]); ++i) cout<<x[i]<<' '; cout<<endl;}
#define DBGV(x) {cout << #x <<": "; for (int i=0; i<(int)Size(x); ++i) cout<<x[i]<<' '; cout<<endl;}

const string problem_name = "bignum";
const int MAXK = 100000;

int was[MAXK][10];

string getans(int mod, int dig){
   string res = "";
   
   do {
      int newmod = was[mod][dig]/100;
      int newdig = was[mod][dig]%10;
      res += (char)(dig+'0');
      mod = newmod;
      dig = newdig;
   } while (mod + dig > 0);
   
   reverse(All(res));
   return res;
}

string solve(string digits, int k){
   int v[10]; Fill(v,0);
   RepV(i,digits) v[digits[i]-'0'] = 1;
   
   Fill(was,0);
   queue<int> q;
   q.push(0);
   
   while (!q.empty()){
      
      int cur = q.front(); q.pop();
      int mod = cur/10;
      int dig = cur%10;
      
      For(i,0,9) if (v[i] && i!=dig) {
         int newmod = (mod*10+i)%k; 
         if (was[newmod][i] == 0){
            was[newmod][i] = mod*100 + 10 + dig;
            
            if (newmod == 0) return getans(newmod,i);
            
            q.push(newmod*10+i);
         }
      }
   }
   
   return "-1";
}

int main(){
//   freopen((problem_name+".in").c_str(),"rt",stdin);
  // freopen((problem_name+".out").c_str(),"wt",stdout);
   
   // Input
   
   string digits;
   int k;
   
   getline(cin,digits);
   cin >> k;
   
   // Check input
   
   assert( k >= 1 && k <= MAXK);
   assert(Size(digits) >= 0 && Size(digits) < 11);
   RepV(i,digits) assert(isdigit(digits[i]));
   For(i,1,Size(digits)-1) assert(digits[i] > digits[i-1]);
   
   // Solve
      
   string res = solve(digits,k);
   
   // Output
   
   cout << res;
   
   return 0;
}
