#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int n;
string A, B;
long long max_gcd = 0;

// Funkcja obliczająca NWD
long long gcd(long long a, long long b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

// Rekurencyjna funkcja przeszukująca wszystkie kombinacje znaków zapytania
void solve(int index, string &a, string &b) {
    if (index == 2 * n) {
        long long num_a = stoll(a);
        long long num_b = stoll(b);
        max_gcd = max(max_gcd, gcd(num_a, num_b));
        return;
    }

    int i = index / n;
    int pos = index % n;

    if (i == 0) {
        if (A[pos] == '?') {
            for (char c = '0'; c <= '9'; ++c) {
                a[pos] = c;
                solve(index + 1, a, b);
            }
        } else {
            a[pos] = A[pos];
            solve(index + 1, a, b);
        }
    } else {
        if (B[pos] == '?') {
            for (char c = '0'; c <= '9'; ++c) {
                b[pos] = c;
                solve(index + 1, a, b);
            }
        } else {
            b[pos] = B[pos];
            solve(index + 1, a, b);
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    if (!(cin >> n)) return 0;
    cin >> A >> B;

    string a = A;
    string b = B;

    solve(0, a, b);

    cout << max_gcd << "\n";

    return 0;
}