/*
#pragma GCC optimize("O2")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2,fma")
//*/
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int > pii;
const ll maxn  = 1005;
const ll mod =1e9+7;
const ld PI = acos((ld)-1);

#define pb push_back
#define endl '\n'
#define dokme(x) cout << x , exit(0)
#define migmig ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define ms(x , y) memset(x , y , sizeof x)
ll q_pow(ll a,int k)
{
    ll ans=1;
    while(k)
    {
        if(k&1)
            ans=(ans*a)%mod;
        a=(a*a)%mod;
        k>>=1;
    }
    return ans;
}
int n;
char aa, ab, ba, bb;
ll dp[maxn];

int32_t main()
{
    cin >> n >> aa >> ab >> ba >> bb;
    if(n <= 3)
        dokme(1);
    if(ab == 'B')
    {
        if(bb == 'B')dokme(1);
        if(ba == 'A')dokme(q_pow(2ll, n - 3));
        dp[2]=1;
        dp[3]=1;
        for(int i=4; i<=n; i++)
            dp[i]=(dp[i-1]+dp[i-2])%mod;
        dokme(dp[n]);
    }
    else
    {
        if(aa == 'A')dokme(1);
        if(ba == 'B')dokme(q_pow(2ll, n - 3));
        dp[2]=1;
        dp[3]=1;
        for(int i=4; i<=n; i++)
            dp[i]=(dp[i-1]+dp[i-2])%mod;
        dokme(dp[n]);
    }
    return(0);
}

/*
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
*/
#include<bits/stdc++.h>
#define ALL(x) (x).begin(),(x).end()
#define ll long long
#define db double
#define ull unsigned long long
#define pii_ pair<int,int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define show1(a) cout<<#a<<" = "<<a<<endl
#define show2(a,b) cout<<#a<<" = "<<a<<"; "<<#b<<" = "<<b<<endl
using namespace std;
const ll INF = 1LL<<60;
const int inf = 1<<30;
const int maxn = 2e5+5;
inline void fastio() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
int a[maxn],p[maxn],x[maxn];

int main()
{
    fastio();
    int n; cin >> n;
    rep(i,1,n) cin >> a[i] >> p[i] >> x[i];
    int ans = inf;
    rep(i,1,n){
        if(a[i] <= (x[i]-1) ) ans = min(ans,p[i]);
    }
    if(ans==inf) cout << -1 << endl;
    else cout <<ans<<endl;
    return 0;
}
