using System.Collections.Generic;
using System.IO;
using System.Text;
using System;

public class Solution
{
    public static string Solve(string s, int k)
    {
        int[,] was = new int[k,10];
        bool[] can = new bool[10];

        foreach (char c in s)
        {
            can[c - '0'] = true;
        }

        Queue<int> q = new Queue<int>();
        q.Enqueue(0);
        q.Enqueue(0);

        while (q.Count > 0)
        {
            int mod = q.Dequeue();
            int dig = q.Dequeue();

            for (int i = 0; i < 10; ++i)
                if (can[i] && i != dig)
                {
                    int newmod = (mod*10 + i)%k;
                    if (was[newmod, i] == 0)
                    {
                        was[newmod, i] = mod*100 + 10 + dig;

                        if (newmod == 0)
                        {
                            StringBuilder res = new StringBuilder(0, k);
                            do
                            {
                                res.Append((char) (i + '0'));
                                mod = was[newmod, i]/100;
                                dig = was[newmod, i]%10;
                                newmod = mod;
                                i = dig;
                            } while (newmod + i > 0);
                            StringBuilder realres = new StringBuilder(0, k);
                            for (int j = res.Length - 1; j >= 0; --j)
                            {
                                realres.Append(res[j]);
                            }
                            return realres.ToString();
                        }

                        q.Enqueue(newmod);
                        q.Enqueue(i);
                    }
                }
        }

        return "-1";
    }

    public static void Main(string[] args)
    {
        //using (StreamReader input = )
        {
            string s = Console.ReadLine();
            int k = int.Parse(Console.ReadLine());

            {
                Console.Write(Solve(s, k));
            }
        }
    }
}