#include<bits/stdc++.h>
using namespace std;
#define ll           long long
#define rep(i,j,n)   for(ll i=j;i<n;i++)
#define scn(a)       scanf("%lld",&a)
#define scns(a,b)    scanf("%lld %lld",&a,&b)
#define print(a)     printf("%lld\n",a)
#define vec          vector<ll>
#define pb           push_back    
#define pairs        pair<ll,ll>
#define f            first
#define s            second
#define all(v)       v.begin(),v.end()
#define mem(a,b)     memset(a,b,sizeof(a))
#define tc 			ll t;scanf("%lld",&t); while(t--)
#define inf          1e18
const int mod = 1e9+7;
const int N = 5e5+5;
int main() 
{
    ll n;
    scn(n);
    ll a[n],p[n],x[n];
    rep(i,0,n)
    cin>>a[i]>>p[i]>>x[i];

    rep(i,0,n)
    a[i]*=60;
    ll ans=1e10;

    rep(i,0,n)
    {
        ll mn=a[i]/60;
        x[i]-=mn;
        if(x[i]>0)
        ans=min(p[i],ans);
    }

    if(ans==1e10)
    ans=-1;
    cout<<ans;
}
//#include <bits/stdc++.h>
#include <cstdio>
//#include <cmath>
//#include <algorithm>
//#include <map>
//#include <iostream>
//#include <string>
//#include <string.h>
//#include <bitset>
//#define _GLIBCXX_DEBUG
//#include <vector>
//#include <regex>
using namespace std;

int N;

int main()
{
  scanf("%d", &N);
  int A, P, X;
  int ans = __INT_MAX__;
  for (int i = 0; i < N; i++)
  {
    scanf("%d%d%d", &A, &P, &X);
    if (A < X)
      if (P < ans)
        ans = P;
  }
  if (ans == __INT_MAX__)
    printf("-1\n");
  else
    printf("%d\n", ans);
  return 0;
}
