#include <iostream>

using namespace std;

long long int N;
long long int A[100000];
long long int P[100000];
long long int X[100000];


long long int cost_min = 1000000000;

void solve() {
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> A[i] >> P[i] >> X[i];
    long long int zaiko = X[i] - A[i];
    if (zaiko > 0) {
      if (cost_min > P[i]) {
        cost_min = P[i];
      }
    }
  }

  if (cost_min == 1000000000) {
    cout << -1 << endl;
  } else {
    cout << cost_min << endl;
  }
}

int main() {
  solve();
  return 0;
}
#include<bits/stdc++.h>
namespace imzzy{
	#define endl '\n'
	#define rgi register int
	typedef long long ll;
	class fastin{private:int _ch,_f;
	public:inline fastin&operator>>(char&c){c=getchar();return*this;}
	template<typename _Tp>inline fastin&operator>>(_Tp&_x){
	_x=0;while(!isdigit(_ch))_f|=(_ch==45),_ch=getchar();
	while(isdigit(_ch))_x=(_x<<1)+(_x<<3)+(_ch^48),_ch=getchar();
	_f&&(_x=-_x,_f=0);return*this;}fastin(){_ch=_f=0;}
	}fin;class fastout{private:int _num[32],_head;
	public:inline fastout&operator<<(char c){putchar(c);return*this;}
	template<typename _Tp> inline fastout&operator<<(_Tp _x){
	_Tp _k;if(_x==0){putchar('0');return *this;}if(_x<0)putchar('-'),_x=-_x;
	while(_x>0)_k=_x/10,++_head,_num[_head]=(_x-(_k<<1)-(_k<<3))^48,_x=_k;
	while(_head>0)putchar(_num[_head]),--_head;return*this;}fastout(){_head=0;}
	}fout;inline void P_INIT(){
	#ifdef D_STDOUT_UNBUFFERED
	setbuf(stdout,NULL);
	#endif
}}using namespace imzzy;
// ----------------------------
// #define int ll
// using namespace std;
const int mod=1000000007,inf=1201201201;
const int maxn=200004;

std::string s;
signed main()
{P_INIT();
	int t;
	fin>>t;
	while(t--) {
		std::cin>>s;
		if("atcoder"<s) {puts("0"); continue;}
		int ans=-1;
		for(rgi i=1,len=s.length();i<len&&ans==-1;++i) {
			if(s[i]>'a') ans=i;
			if(s[i]>'t') ans=i-1;
		} fout<<ans<<endl;
	}
	return 0;
}
// ----------------------------
// by imzzy