#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rep2(i, s, n) for (int i = (s); i < (n); i++)
using namespace std;
using ll = long long;
using P = pair<int, int>;
int main() {
  int n;
  cin >> n;
  int m = 5;
  vector<vector<int>> men((n), vector<int>(m));
  rep(i, n) rep(j, m) cin >> men[i][j];
  int l = 0, r = 1001001001;  // 100 100 100 1をすると10^9というのがわかりやすい
  while (l + 1 < r) {
    int c = (l + r) / 2;
    vector<int> s;
    rep(i, n) {
      int x = 0;
      rep(j, m) {
        if (men[i][j] >= c) x |= 1 << j;  // jbit目にbitを立てに行く
      }
      s.push_back(x);
    }
    sort(s.begin(), s.end());
    s.erase(unique(s.begin(), s.end()), s.end());
    bool ok = false;
    rep(i, s.size()) rep(j, i + 1) rep(k, j + 1) {  // k<=j<=i
      if ((s[i] | s[j] | s[k]) == (1 << m) - 1)
        ok = true;  //(1<<m)-1でmbitの1...1が作れる
    }
    if (ok)
      l = c;
    else
      r = c;
  }
  cout << l << endl;
  return 0;
}
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <cassert>
#include <iomanip>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define rALL(a) (a).rbegin(), (a).rend()
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const int iINF = 1001001001;
const long long llINF = 1LL << 60;
int main()
{
    //int a, b; cin >> a >> b;
    //cout << (a + b) / 2 << " " << (a - b) / 2 << endl;

    /*//cin >> n >> s;
    int n = 5000; string s(n, 'T');
    ll ans = 0;
    for (int i = 2; i <= n; i += 2) {
        for (int j = 0; j < n - i + 1; ++j) {
            int a, t, g, c;
            a = t = g = c = 0;
            rep(k, i) {
                a += s[j + k];
                //if (s[j + k] == 'A') a++;
                //if (s[j + k] == 'T') t++;
                //if (s[j + k] == 'G') g++;
                //if (s[j + k] == 'C') c++;
            }
            if (a == t && g == c) ans++;
        }
    }
    cout << ans << endl;
    */
    
    int n; string s;
    cin >> n >> s;
    ll ans = 0;
    vector<ll> sum(n + 1);
    sum[0] = 0;
    rep(i, n) {
        int num = 0;
        if (s[i] == 'A') num = 1;
        if (s[i] == 'T') num = -1;
        if (s[i] == 'G') num = 10000;
        if (s[i] == 'C') num = -10000;
        sum[i + 1] = sum[i] + num;
    }
    rep(i, n + 1) {
        for (int j = i + 1; j < n + 1; ++j) {
            if (sum[j] - sum[i] == 0) {
                //cout << i << "(" << sum[i] << "), " << j << "(" << sum[j] << ")" << endl;
                ans++;
            }
        }
    }
    cout << ans << endl;
    

    //vector<int> pos;
    //for (int i = 0; i < n - 1; ++i) {
    //    bool ok = false;
    //    if (s[i] == 'A' && s[i + 1] == 'T') ok = true;
    //    if (s[i] == 'T' && s[i + 1] == 'A') ok = true;
    //    if (s[i] == 'C' && s[i + 1] == 'G') ok = true;
    //    if (s[i] == 'G' && s[i + 1] == 'C') ok = true;
    //    if (ok) pos.push_back(i);
    //}
    //ans = pos.size();
    //int sz = pos.size();
    //rep(i, sz) {
    //    for (int j = i + 1; j < sz; ++j) {
    //        if (2 < pos[j] - pos[j - 1]) break;
    //        ans++;
    //    }
    //}
    //cout << ans << endl;
    return 0;
}