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

#define rep(i,n) for(int i=0; i<(n); i++)
#define rep2(i,x,n) for(int i=x; i<(n); i++)
#define all(x) x.begin(),x.end()
typedef long long ll;
ll mod = 1000000007;
ll inf = 1e18;
int main(){

    int n;
    cin >> n;
    vector<int> a(n),b(n),c(n),d(n),e(n);
    rep(i,n) cin >> a[i] >> b[i] >> c[i] >> d[i] >> e[i];
    vector<int> ftest(n,0),ff(n,0);
    
    rep(i,n){
        for(int j=i+1; j<n; j++){
            if(a[i]>=a[j] && b[i]>=b[j] && c[i]>=c[j] && d[i]>=d[j] && e[i]>=e[j]) ftest[j]--;
            if(a[i]<=a[j] && b[i]<=b[j] && c[i]<=c[j] && d[i]<=d[j] && e[i]<=e[j]) ftest[i]--;

            if(a[i]==a[j] && b[i]==b[j] && c[i]==c[j] && d[i]==d[j] && e[i]==e[j]){
                ff[i]++;ftest[j]=-3; ff[j]=-10000;
            }
        }
    }

    vector<int> fc;
    rep(i,n) {
        if(ftest[i]>=-2) fc.push_back(i);
        if(ff[i]==1){
            rep(i,2) fc.push_back(i);
        }
        if(ff[i]>=2){
            rep(i,3) fc.push_back(i);
        }
    }

    int m=fc.size();
    // cout << m << endl;
    int ans=1;

    rep(i,m){
        for(int j=i+1; j<m; j++){
            for(int k=j+1; k<m; k++){
                int pp=max(a[fc[i]],max(a[fc[j]],a[fc[k]]));
                int ss=max(b[fc[i]],max(b[fc[j]],b[fc[k]]));
                int tt=max(c[fc[i]],max(c[fc[j]],c[fc[k]]));
                int cc=max(d[fc[i]],max(d[fc[j]],d[fc[k]]));
                int hh=max(e[fc[i]],max(e[fc[j]],e[fc[k]]));
                int allp=min(pp,min(ss,min(tt,min(cc,hh))));
                ans=max(ans,allp);
            }
        }
    }
    cout << ans << endl;
    return 0;
}
#include <bits/stdc++.h>
using namespace std;
#ifdef tabr
#include "library/debug.cpp"
#else
#define debug(...)
#endif

template <long long mod>
struct modular {
    long long value;
    modular(long long x = 0LL) {
        value = x % mod;
        if (value < 0LL) value += mod;
    }
    modular& operator+=(modular other) {
        if ((value += other.value) >= mod) value -= mod;
        return *this;
    }
    modular& operator-=(modular other) {
        if ((value -= other.value) < 0LL) value += mod;
        return *this;
    }
    modular& operator*=(modular other) {
        value = value * other.value % mod;
        return *this;
    }
    modular& operator/=(modular other) {
        long long a = 0LL, b = 1LL, c = other.value, m = mod;
        while (c != 0LL) {
            long long t = m / c;
            m -= t * c;
            swap(c, m);
            a -= t * b;
            swap(a, b);
        }
        a %= mod;
        if (a < 0LL) a += mod;
        value = value * a % mod;
        return *this;
    }
    modular operator-() { return modular(-value); }
    modular operator+(modular rhs) { return modular(*this) += rhs; }
    modular operator-(modular rhs) { return modular(*this) -= rhs; }
    modular operator*(modular rhs) { return modular(*this) *= rhs; }
    modular operator/(modular rhs) { return modular(*this) /= rhs; }
    bool operator==(modular rhs) { return value == rhs.value; }
    bool operator!=(modular rhs) { return value != rhs.value; }
    bool operator<(modular rhs) { return value < rhs.value; }
};
template <long long mod>
string to_string(modular<mod> x) {
    return to_string(x.value);
}
template <long long mod>
ostream& operator<<(ostream& stream, modular<mod> x) {
    x.value %= mod;
    if (x.value < 0LL) x.value += mod;
    return stream << x.value;
}
template <long long mod>
istream& operator>>(istream& stream, modular<mod>& x) {
    stream >> x.value;
    x.value %= mod;
    if (x.value < 0LL) x.value += mod;
    return stream;
}
const long long mod = (long long)1e9 + 7;
using mint = modular<mod>;

inline mint pw(mint a, long long n) {
    mint res = 1;
    while (n > 0LL) {
        if (n & 1LL) {
            res *= a;
        }
        a *= a;
        n >>= 1;
    }
    return res;
}
vector<mint> fact, finv;
inline void cinit(int n) {
    fact.resize(n, mint(1LL));
    finv.resize(n, mint(1LL));
    for (int i = 2; i < n; i++) {
        fact[i] = fact[i - 1] * i;
    }
    finv[n - 1] /= fact[n - 1];
    for (int i = n - 2; i >= 2; i--) {
        finv[i] = finv[i + 1] * (i + 1);
    }
}
inline mint C(int n, int k) {
    if (n < k || k < 0 || n < 0) {
        return mint(0LL);
    }
    return fact[n] * finv[k] * finv[n - k];
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<vector<int>> c(2, vector<int>(2));
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            char x;
            cin >> x;
            c[i][j] = x - 'A';
        }
    }
    if (n <= 3) {
        cout << 1 << '\n';
        return 0;
    }
    if (c[0][1] == 1) {
        swap(c[0][0], c[1][1]);
        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 2; j++) {
                c[i][j] ^= 1;
            }
        }
    }
    if (c[0][0] == 0) {
        cout << 1 << '\n';
    } else if (c[1][0] == 0) {
        vector<vector<mint>> dp(2, vector<mint>(n));
        dp[0][0] = 1;
        for (int i = 1; i < n; i++) {
            dp[0][i] = dp[0][i - 1] + dp[1][i - 1];
            dp[1][i] = dp[0][i - 1];
        }
        cout << dp[1][n - 1] << '\n';
    } else {
        cout << pw(2, n - 3) << '\n';
    }
    return 0;
}