#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int a, b;

int sum(int a) {
    int ans = 0;
    while (a > 0) {
        ans += a%10;
        a /= 10;
    }
    return ans;
}

int main() {                       
    freopen("mcd.in", "r", stdin);
    freopen("mcd.out", "w", stdout);
    scanf("%d%d", &a, &b);
    int ans = 1;
    for (int i = 1; (ll)i*i <= a; ++i) {
        if (a % i != 0) continue;
        int j = a/i;
        if (b % i == 0 && sum(ans) < sum(i)) ans = i;
        if (b % j == 0 && sum(ans) < sum(j)) ans = j;
    }
    printf("%d\n", ans);
    return 0;
}