//
// Written by Fyodor Menshikov 25.02.2008
// 1:33-2:07
//

import java.io.*;
import java.util.*;

class Pair {
   int rem;
   int dig;

   Pair(int rem, int dig) {
      this.rem = rem;
      this.dig = dig;
   }
}

public class Main {

   Scanner in;
   PrintWriter out;

   Main() {
      try {
         in = new Scanner(System.in);
         out = new PrintWriter(System.out);
      } catch (Exception e) {
         throw new Error();
      }
   }

   void asserT(boolean e) {
      if (!e) {
         throw new Error();
      }
   }

   void run() {
      String set = in.nextLine();
      boolean isAllowed[] = new boolean[10];
      for (int i = 0; i < set.length(); i++) {
         int d = set.charAt(i) - '0';
         asserT(0 <= d && d <= 9);
         asserT(!isAllowed[d]);
         isAllowed[d] = true;
      }
      int k = in.nextInt();
      asserT(1 <= k && k <= 100000);
      Pair from[][] = new Pair[k][10];
      LinkedList<Pair> queue = new LinkedList<Pair>();
      queue.add(new Pair(0, 0));
      int resDig = -1;
      while (resDig == -1) {
         if (queue.size() == 0) {
            out.println("-1");
            out.close();
            return;
         }
         Pair current = queue.poll();
         for (int newDig = 0; newDig <= 9; newDig++) {
            if (isAllowed[newDig] && newDig != current.dig) {
               int newRem = (current.rem * 10 + newDig) % k;
               if (from[newRem][newDig] == null) {
                  from[newRem][newDig] = current;
                  queue.add(new Pair(newRem, newDig));
                  if (newRem == 0) {
                     resDig = newDig;
                     break;
                  }
               }
            }
         }
      }
      Stack<Integer> stack = new Stack<Integer>();
      int resRem = 0;
      do {
         stack.push(resDig);
         Pair prev = from[resRem][resDig];
         resRem = prev.rem;
         resDig = prev.dig;
      } while (resRem != 0);
      while (stack.size() > 0) {
         out.print(stack.pop());
      }
      out.println();
      out.close();
   }

   public static void main(String args[]) {
      new Main().run();
   }
}