#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <tuple>
#include <numeric>
#include <functional>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> P;
#define rep(i, n) for(ll i = 0; i < n; i++)
#define exrep(i, a, b) for(ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;

// a^n (mod.MOD)を求める。計算量はO(logn)
ll modpow(ll a, ll n, ll MOD = mod) {
    if(n == 0) {
        return 1;
    }
    if(n%2 == 1) {
        return (a * modpow(a, n-1, MOD))%MOD;
    }
    return (modpow(a, n/2, MOD) * modpow(a, n/2, MOD))%MOD;
}

int main() {
    ll n;
    cin >> n;

    char cAA, cAB, cBA, cBB;
    cin >> cAA >> cAB >> cBA >> cBB;

    if(n == 2) {
        out(1);
        re0;
    }

    if( (cAA == 'A' && cAB == 'B' && cBA == 'A' && cBB == 'A') ||
        (cAA == 'B' && cAB == 'A' && cBA == 'B' && cBB == 'A') ||
        (cAA == 'B' && cAB == 'A' && cBA == 'B' && cBB == 'B') ||
        (cAA == 'B' && cAB == 'B' && cBA == 'A' && cBB == 'A')
        ) {
            out(modpow(2LL, n-3));
    }
    else if ( (cAA == 'A' && cAB == 'B' && cBA == 'B' && cBB == 'A') ||
              (cAA == 'B' && cAB == 'A' && cBA == 'A' && cBB == 'A') ||
              (cAA == 'B' && cAB == 'A' && cBA == 'A' && cBB == 'B') ||
              (cAA == 'B' && cAB == 'B' && cBA == 'B' && cBB == 'A') 
        ) { 
            vl f(n);  // f[i] : フィボナッチ数列のi項目
            f[1] = f[2] = 1;
            exrep(i, 3, n-1) {
                f[i] = (f[i-1] + f[i-2])%mod;
            }
            out(f[n-1]);
    }
    else {
        out(1);
    }

    re0;
}
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
long long ksm(long long x,long long y)//快速幂
{
    if(x==0) return 0; 
    long long ret=1;
    for(;y;y>>=1)
    {
        if(y&1) ret=(ret*x);
        x=(x*x);
    }
    return (ret);//最后%一下 
} 
int main()
{
	ll n;
	scanf("%lld",&n);
	for(int i=1;i<=40;i++)
		for(int j=1;j<=27;j++)
		{
			if(ksm(3,i)+ksm(5,j)==n)
			{
				printf("%d %d\n",i,j);
				return 0;
			}
			}	
	printf("-1\n");
	return 0;
}
