#include <bits/stdc++.h>
using namespace std;

bool isPrime(int x) {
    if (x < 2) return false;
    for (int i = 2; i * i <= x; i++)
        if (x % i == 0) return false;
    return true;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    string n;
    long long Y;
    cin >> n >> Y;

    vector<int> cnt(10, 0);
    long long S = 0;
    for (char c : n) {
        cnt[c - '0']++;
        S += c - '0';
    }

    long long A = 0, tmp = Y;
    while (tmp) {
        A += tmp % 10;
        tmp /= 10;
    }

    double x = 21.268443;
    double y = 105.204557;

    double x0 = S + (A % 10);

    string best = "";

    for (int a = 9; a >= 0; a--) {
        if (cnt[a] == 0) continue;
        cnt[a]--;

        for (int b = 9; b >= 0; b--) {
            if (cnt[b] == 0) continue;
            cnt[b]--;

            int R = 10 * b + a;
            double y0 = (R + A) % 100;

            double d = sqrt((x0 - x)*(x0 - x) + (y0 - y)*(y0 - y));
            int K = (int)d;

            if (isPrime(K)) {
                string res = "";
                res += char('0' + a);
                res += char('0' + b);

                for (int d = 9; d >= 0; d--) {
                    res += string(cnt[d], char('0' + d));
                }

                if (res[0] != '0') {
                    cout << res;
                    return 0;
                }
            }

            cnt[b]++;
        }

        cnt[a]++;
    }

    cout << -1;
}
