//#pragma GCC optimize ("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
#define pb push_back
#define F first
#define S second
#define ins insert
#define mp make_pair
#define fo(i, n1, n, x) for(int i = n1; i <= n; i += x)
#define foo(i, n, n1, x) for(int i = n; i >= n1; i -= x)
#define bit __builtin_popcount
#define md (l + ((r - l) / 2))
#define all(x) x.begin(),x.end()
#define eb emplace_back
#define ub upper_bound
#define lb lower_bound
#define ios ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define file(s) if (fopen(s".in", "r")) freopen(s".in", "r", stdin), freopen(s".out", "w", stdout)

using namespace std;
//using namespace __gnu_pbds;
//using namespace __gnu_cxx;

using ll = long long;

//#define int ll

using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;

const int N = 2e5 + 11, mod = 1e9 + 7, mod2 = 998244353;
const int MAX = 1e5 + 11;
const int INF1 = 2e9 + 11;
const ll INF2 = 6e18 + 11;
const double INF3 = 1e8 + 11;
const int base = 500;
const int P = 31;
const int dx[] = {1, -1, 0, 0, 1, 1, -1, -1};
const int dy[] = {0, 0, 1, -1, 1, -1, 1, -1};
const double EPS = 1e-4;
const double PI = acos(-1.0);

//template<typename T> using ordered_set = tree <T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T1, typename T2> inline void chmin(T1 &a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1 &a, T2 b) { if (a < b) a = b; }

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int rnd(int l, int r) { return uniform_int_distribution<int> (l, r)(rng); }

int r, c, a[507][507], b[507][507], d[507][507][2];
main() {
  file("paint");
  ios;
  cin >> r >> c;
  fo(i, 1, r, 1) fo(j, 1, c - 1, 1) cin >> a[i][j];
  fo(i, 1, r - 1, 1) fo(j, 1, c, 1) cin >> b[i][j];
  fo(i, 1, r, 1) fo(j, 1, c, 1) d[i][j][0] = d[i][j][1] = INF1;
  d[1][1][0] = 0;
  priority_queue < pair <pii, pii> > q;
  q.push(mp(mp(0, 0), mp(1, 1)));
  function<bool(int, int)> valid = [&](int x, int y) { return (1 <= x and x <= r and 1 <= y and y <= c); };
  while (!q.empty()) {
    pair <pii, pii> v = q.top();
    q.pop();
    int nx, ny;
    int t = v.F.S;
    if (-v.F.F != d[v.S.F][v.S.S][t]) continue;
    nx = v.S.F;
    ny = v.S.S + 1;
    if (valid(nx, ny) and d[nx][ny][0] > d[v.S.F][v.S.S][t] + a[v.S.F][v.S.S]) {
      d[nx][ny][0] = d[v.S.F][v.S.S][t] + a[v.S.F][v.S.S];
      q.push(mp(mp(-d[nx][ny][0], 0), mp(nx, ny)));
    }
    nx = v.S.F;
    ny = v.S.S - 1;
    if (valid(nx, ny) and d[nx][ny][0] > d[v.S.F][v.S.S][t] + a[v.S.F][v.S.S - 1]) {
      d[nx][ny][0] = d[v.S.F][v.S.S][t] + a[v.S.F][v.S.S - 1];
      q.push(mp(mp(-d[nx][ny][0], 0), mp(nx, ny)));
    }
    nx = v.S.F + 1;
    ny = v.S.S;
    if (valid(nx, ny) and d[nx][ny][0] > d[v.S.F][v.S.S][t] + b[v.S.F][v.S.S]) {
      d[nx][ny][0] = d[v.S.F][v.S.S][t] + b[v.S.F][v.S.S];
      q.push(mp(mp(-d[nx][ny][0], 0), mp(nx, ny)));
    }
    if (t) {
      nx = v.S.F - 1;
      ny = v.S.S;
      if (valid(nx, ny) and d[nx][ny][t] > d[v.S.F][v.S.S][t] + 1) {
        d[nx][ny][t] = d[v.S.F][v.S.S][t] + 1;
        q.push(mp(mp(-d[nx][ny][t], t), mp(nx, ny)));
      }
    } else {
      nx = v.S.F;
      ny = v.S.S;
      t ^= 1;
      if (valid(nx, ny) and d[nx][ny][t] > d[v.S.F][v.S.S][1-t] + 1) {
        d[nx][ny][t] = d[v.S.F][v.S.S][1-t] + 1;
        q.push(mp(mp(-d[nx][ny][t], t), mp(nx, ny)));
      }
    }
  }
  cout << min(d[r][c][0], d[r][c][1]);
  return 0;
}
/**

*/

#include <bits/stdc++.h>

#define eprintf(args...) fprintf(stderr, args)
#define rep(i, n) for (int i = 0; i < (int)(n); ++ i)

const int mxn = 2e5 + 5;
int n, a[mxn], b[mxn];
std::map <int, std::vector <int> > oa, ob;

int s[mxn];

void update(int x, int v) {
	for (++ x; x < mxn; x += x & -x) s[x] += v;
}

int query(int x) {
	int ans = 0;
	for (++ x; x; x -= x & -x) ans += s[x];
	return ans;
}

int frm[mxn];

int main() {
	scanf("%d", &n);
	rep(i, n) scanf("%d", &a[i]), a[i] += i;
	rep(i, n) scanf("%d", &b[i]), b[i] += i;
	rep(i, n) oa[a[i]].push_back(i);
	rep(i, n) ob[b[i]].push_back(i);
	for (auto x : oa) {
		std::vector <int> va = x.second, vb = ob[x.first];
		if (va.size() != vb.size()) return printf("%d\n", -1), 0;
		rep(i, va.size()) frm[vb[i]] = va[i];
	}
	rep(i, n) update(i, 1);
	long long ans = 0;
	rep(i, n) ans += query(frm[i] - 1), update(frm[i], -1);
	printf("%lld\n", ans);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define int long long

const int N = 1050;

vector<int> g[N][26];
bool vis[N][N];
int dp[N][N];

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int n, m;
    cin >> n >> m;

    for (int i = 0; i < m; i++) {
        int a, b;
        char c;
        cin >> a >> b >> c;

        g[a][c - 'a'].push_back(b);
        g[b][c - 'a'].push_back(a);
    }

    fill(&dp[0][0], &dp[n + 1][0], LLONG_MAX);

    queue<pair<int, int> > bfs;
    for (int i = 1; i <= n; i++) {
        bfs.emplace(i, i);
        vis[i][i] = true;
        dp[i][i] = 0;
    }

    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < 26; j++) {
            for (auto to: g[i][j]) {
                if (vis[i][to])
                    continue;
                bfs.emplace(i, to);
                vis[i][to] = vis[to][i] = true;
                dp[i][to] = dp[to][i] = 1;
            }
        }
    }

    while (!bfs.empty()) {
        pair<int, int> nodes = bfs.front();
        bfs.pop();
        int u = nodes.first;
        int v = nodes.second;

        for (int j = 0; j < 26; j++) {
            for (auto nu: g[u][j]) {
                for (auto nv: g[v][j]) {
                    if (vis[nu][nv])
                        continue;
                    vis[nu][nv] = vis[nv][nu] = true;
                    dp[nu][nv] = dp[nv][nu] = dp[u][v] + 2;
                    bfs.emplace(nu, nv);
                }
            }
        }
    }
    cout << (dp[1][n] == LLONG_MAX ? -1 : dp[1][n]);

}

#pragma GCC optimize("O3")
#pragma GCC target("sse4")

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned int uint;
typedef pair<int, int> pii;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;

#define FOR(i, a, b) for (int i = a; i <= (b); i++)
#define F0R(i, a) for (int i = 0; i < (a); i++)
#define FORd(i, a, b) for (int i = (b); i >= a; i--)
#define F0Rd(i, a) for (int i = (a)-1; i >= 0; i--)
#define trav(a, x) for (auto &a : x)

#define sz(x) (int)(x).size()
#define popcnt(x) __builtin_popcount(x)
#define low_bo(a, x) (lower_bound(a.begin(), a.end(), x) - a.begin())
#define up_bo(a, x) (upper_bound(a.begin(), a.end(), x) - a.begin())
#define unique(a) a.resize(unique(a.begin(), a.end()) - a.begin())
#define shuffle(a) shuffle(a.begin(), a.end(), rnd)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define ins insert

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const int MOD = 1000000007;
const char nl = '\n';
const int MX = 150001; //check the limits, dummy
set<pii> visited;
void solve(int t) {
  int n, m;
  cin>>n >> m;
  map<int, map<int, char>> adj;
  F0R(i,m) {
    int u,v;
    char c;
    cin>>u>>v>>c;
    u--, v--;
    adj[u][v]=c;
    adj[v][u]=c;
  }
  // negative length of string
  priority_queue<pair<ll, pii>> pq;
  pq.push(mp(0,mp(0,n-1)));
  int u,v;
  set<int> pos;
  while(!pq.empty()) {
    auto cur = pq.top();
    pq.pop();
    ll l = -cur.f;
    tie(u,v) = cur.s;
    // cout << l <<" "<<st<<" "<<u<<" "<<v<<nl;
    if(visited.find({u,v})!=visited.end()) {
      // cout << "hi\n";
      continue;
    }
    visited.ins({u,v});
    if(u==v) {
      pos.ins(2*l);
      // cout << 2*l<<nl;
      // return;
    }
    if(adj[u].find(v)!=adj[u].end()) {
      pos.ins(2*l+1);
      // cout << 2*l+1<<nl;
      // return;
    }
    for(auto au : adj[u]) {
      for(auto av : adj[v]) {
        if(au.s==av.s) {
          pq.push(mp(-l-1,mp(au.f,av.f)));
        }
      }
    }
  }
  if(!pos.empty()) cout << *(pos.begin())<<nl;
  else
  cout << "-1\n";
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout.precision(20);
  cout << fixed;

  int T = 1;
  F0R(i,T) {
    solve(i+1);
  }
}

// read the question correctly (ll vs int)
// template by bqi343


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
}

const int maxN = 3e5 + 5;
int a[maxN] = {0};
int n;

void update(int idx, int val) {
    while (idx <= n) {
        a[idx] ^= val;
        idx += (idx & -idx);
    }
}

int query(int idx) {
    int ans = 0;
    while (idx > 0) {
        ans ^= a[idx];
        idx -= (idx & -idx);
    }
    return ans;
}

int32_t main()
{
    init();

    int q;
    cin >> n >> q;

    for (int i = 0; i < n; ++i) {
        int x;
        cin >> x;
        update(i + 1, x);
    }

    while (q--) {
        int t, x, y;
        cin >> t >> x >> y;
        if (t == 1) {
            update(x, y);
        } else {
            cout << (query(y) ^ query(x - 1)) << "\n";
        }
    }

    return 0;
}

#include <bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, a, b) for (int i = (int)(a); i >= (int)b; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)
#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define ms(x, y) memset(x, y, sizeof(x))
#define SZ(x) int(x.size())
#define fk cerr<<"fk"<<endl
#define db(x) cerr<<(#x)<<'='<<(x)<<endl
using namespace std;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long i64;
typedef vector<i64> vi64;
typedef vector<vi64> vvi64;
typedef pair<i64, i64> pi64;
typedef double ld;
template<class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }
//1.integer overflow (1e5 * 1e5) (2e9 + 2e9)
//2.runtime error
//3.boundary condition
const int N=300000+100;
int n,q,a[N],tv[N*4];
void build(int node,int l,int r){
	if(l==r){
		tv[node]=a[l];
		return;
	}
	int mid=(l+r)>>1;
	build(node<<1,l,mid);
	build(node<<1|1,mid+1,r);
	tv[node]=tv[node<<1]^tv[node<<1|1];
}
void update(int node,int l,int r,int idx,int val){
	if(l==r){
		int tt=val^a[l];
		tv[node]=tt;
		a[l]=tt;
		return;
	}
	int mid=(l+r)>>1;
	if(idx<=mid) update(node<<1,l,mid,idx,val);
	else update(node<<1|1,mid+1,r,idx,val);
	tv[node]=tv[node<<1]^tv[node<<1|1];
}
int query(int node,int l,int r,int ql,int qr){
	if(ql<=l && r<=qr){
		return tv[node];
	}
	int ret=0;
	int mid=(l+r)>>1;
	if(ql<=mid) ret^=query(node<<1,l,mid,ql,qr);
	if(qr>mid) ret^=query(node<<1|1,mid+1,r,ql,qr);
	return ret;
}
int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(10);
    cout<<fixed;
#ifdef LOCAL_DEFINE
    freopen("input.txt", "r", stdin);
#endif
    cin>>n>>q;
    for1(i, n){
    	cin>>a[i];
    }
    build(1,1,n);
    forn(i, q){
		int t,x,y;
		cin>>t>>x>>y;
		if(t==1) update(1,1,n,x,y);
		else cout<<query(1,1,n,x,y)<<'\n';
	}
#ifdef LOCAL_DEFINE
    cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
#endif
    return 0;
}


// Author: Muhesh Kumar

#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using db = long double;
using pi = pair<int, int>;
using pl = pair<ll, ll>;

using vi = vector<int>;
using vl = vector<ll>;
using vb = vector<bool>;
using vs = vector<string>;
using vpi = vector<pi>;
using vpl = vector<pl>;
using vvi = vector<vi>;
using vvl = vector<vl>;
using vvb = vector<vb>;

#define nl '\n'
#define fr first
#define sc second
#define bk back()
#define ft front()
#define pb push_back
#define ppb pop_back
#define lb lower_bound
#define ub upper_bound
#define bg(x) begin(x)
#define all(x) bg(x), end(x)
#define sz(x) (int) (x).size()
#define amax(a, b) a = max(a, b)
#define amin(a, b) a = min(a, b)
#define trav(a, b) for(auto &a: b)
#define rep(i, a, b) for(int i = a; i < b; i++)

const ll inf = 1e18 + 10;
const db eps = 1e-9;
const int mod = 1e9 + 7;

ll modadd(ll a, ll b, ll m = mod) { a %= m; b %= m; return (((a + b) % m) + m) % m; }
ll modsub(ll a, ll b, ll m = mod) { a %= m; b %= m; return (((a - b) % m) + m) % m; }
ll modmul(ll a, ll b, ll m = mod) { a %= m; b %= m; return (((a * b) % m) + m) % m; }
ll gcd(ll a, ll b) { return __gcd(a, b); }
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

template <typename T1, typename T2>
inline std::ostream &operator << (std::ostream& os, const std::pair<T1, T2>& p) {
    return os << "(" << p.first << ", " << p.second << ")";
}

template<typename T>
inline std::ostream &operator << (std::ostream& os,const std::vector<T>& v) {
    bool first = true;
    os << "[";
    for(unsigned int i = 0; i < v.size(); i++) {
        if(!first)
            os << ", ";
        os << v[i];
        first = false;
    }
    return os << "]";
}

template<typename T>
inline std::ostream &operator << (std::ostream& os,const std::vector<vector<T>>& v) {
    bool first = true;
    os << "[\n ";
    for(unsigned int i = 0; i < v.size(); i++) {
        if(!first)
            os << ",\n ";
        os << v[i];
        first = false;
    }
    return os << "\n]";
}

template<typename T>
inline std::ostream &operator << (std::ostream& os,const std::set<T>& v) {
    bool first = true;
    os << "[";
    for (typename std::set<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
        if(!first)
            os << ", ";
        os << *ii;
        first = false;
    }
    return os << "]";
}

template<typename T1, typename T2>
inline std::ostream &operator << (std::ostream& os,const std::map<T1, T2>& v) {
    bool first = true;
    os << "[\n ";
    for (typename std::map<T1, T2>::const_iterator ii = v.begin(); ii != v.end(); ++ii) {
        if(!first)
            os << ",\n ";
        os << *ii ;
        first = false;
    }
    return os << "\n]";
}

#ifndef ONLINE_JUDGE
    #define debug(a) cerr << #a << " " << a << '\n';
#else
    #define debug(a)
#endif

int main() {

    ios::sync_with_stdio(false);
    cin.tie(0);

    int n; cin >> n;
    string s; cin >> s;
    stack<char> st;

    trav (i, s) {
        st.push(i);
        if (sz(st) >= 3) {
            string t;

            int T = 3;
            while (T--) {
                t.pb(st.top());
                st.pop();
            }

            reverse(all(t));
            if (t != "fox") {
                trav (j, t) st.push(j);
            }
        }
    }

    cout << sz(st);

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
int n,t,i;
char s[200005],c[200005];
int main(){
    cin >>n;
    cin >>s;
    for(int i=0;i<n;i++)
        if(s[i]=='x'&&t>1&&c[t]=='o'&&c[t-1]=='f')
            t-=2;
        else
            c[++t]=s[i];
    cout <<t <<"\n";
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define repp(i, st, en) for (ll i = (ll)st; i < (ll)(en); i++)
#define repm(i, st, en) for (ll i = (ll)st; i >= (ll)(en); i--)
#define all(v) v.begin(), v.end()
void chmax(ll &x, ll y) {x = max(x,y);}
void chmin(ll &x, ll y) {x = min(x,y);}
void Yes() {cout << "Yes" << endl; exit(0);}
void No() {cout << "No" << endl; exit(0);}
template<class in_Cout> void Cout(in_Cout x) {cout << x << endl; exit(0);}
template<class in_vec_cout>
void vec_cout(vector<in_vec_cout> vec) {
    for (in_vec_cout res : vec) {cout << res << " ";}
    cout << endl;
}

const ll inf = 1e18;
const ll mod = 1e9 + 7;

int main() {
    ll N, M, Q;
    cin >> N >> M >> Q;
    vector<ll> W(N), V(N);
    rep(i,N) cin >> W[i] >> V[i];
    vector<ll> X(M);
    rep(i,M) cin >> X[i];

    vector<pair<ll,ll>> P(N);
    rep(i,N) P[i] = {V[i],W[i]};
    sort(all(P),greater<pair<ll,ll>>());

    vector<ll> res(Q);
    rep(q,Q) {
        ll l, r; cin >> l >> r;
        l--; r--;
        vector<ll> box;
        rep(i,M) if (i<l || r<i) box.push_back(X[i]);
        sort(all(box));
        ll n = box.size();
        vector<bool> used(n);
        ll tmp = 0;
        rep(i,N) {
            ll v = P[i].first, w = P[i].second;
            rep(j,n) {
                if (used[j]) continue;
                if (w<=box[j]) {
                    tmp += v;
                    used[j] = true;
                    break;
                }
            }
        }
        res[q] = tmp;
    }

    for (ll ans : res) cout << ans << endl;
}

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, n) for(int i=0; i<n; i++)

struct Data {
    ll w, v;
    Data(ll w, ll v) : w(w), v(v) {}
    bool operator < (const Data &d) const {
        if (d.v == v) {
            return d.w > w;
        } else {
            return d.v < v;
        }
    }
};

int main() {
    int N, M, Q;
    cin >> N >> M >> Q;
    vector<Data> d;
    int X[M];
    ll W, V;
    for (int i = 0; i < N; i++) {
        cin >> W >> V;
        d.push_back(Data(W, V));
    }
    for (int i = 0; i < M; i++) {
        cin >> X[i];
    }
    int L, R;
    sort(d.begin(), d.end());
    for (int i = 0; i < Q; i++) {
        cin >> L >> R;
        ll ans = 0;
        L--;
        R--;
        bool flag[M] = {};
        for (int j = 0; j < N; j++) {
            int id = -1;
            for (int k = 0; k < M; k++) {
                if (L <= k && k <= R) continue;
                if (!flag[k] && d[j].w <= X[k]) {
                    if (id == -1) {
                        id = k;
                    } else if (X[k] < X[id]) {
                        id = k;
                    }
                }
            }
            if (id != -1) {
                ans += d[j].v;
                flag[id] = true;
            }
        }
        cout << ans << endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;++i)
#define irep(i,a,b) for(int i=int(a);i<(int)b;++i)
#define rrep(i,a,b) for(int i=int(a);i>=(int)b;--i)
#define vi vector<int>
#define vvi vector<vector<int>>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define vvp vector<vector<pair<ll,ll>>>
#define vpl vector<pair<ll,ll>>
#define vpi vector<pair<int,int>>
#define pb push_back
#define se second
#define fi first
#define all(v) v.begin(),v.end()
#define v(T) vector<T>
#define vv(T) vector<vector<T>>

using namespace std;

template<typename T> istream& operator>>(istream&i,v(T)&v){rep(j,v.size())i>>v[j];return i;}
template<typename T> string join(const v(T)&v){stringstream s;rep(i,v.size())s<<' '<<v[i];return s.str().substr(1);}
template<typename T> ostream& operator<<(ostream&o,const v(T)&v){if(v.size())o<<join(v);return o;}

using ll = long long;
const ll INF = LLONG_MAX-1e9;
const double PI = acos(-1);

const ll mod = 1e9 + 7; //998244353;

template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

ll modpow(ll a,ll b){
    if(b == 0){
        return 1;
    }
    if(b%2 == 0){
        ll tmp = modpow(a,b/2);
        return tmp*tmp%mod;
    }else{
        return modpow(a,b-1)*a%mod;
    }

}

ll c[31][31];
int main(void)
{
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    int a,b;
    cin >> a >> b;
    ll k;
    cin >> k;
    rep(i,31){
        c[i][0] = 1;
        c[0][i] = 1;
    }
    rep(i,30)rep(j,30){
        c[i+1][j+1] = c[i+1][j]*(i+j+2);
        c[i+1][j+1]/= j+1;
    }
    int ad = a+b;
    string ans = "";
    rep(i,ad){
        if(a == 0) ans += 'b';
        else if(c[a-1][b] >= k){
            --a;
            ans += 'a';
        }else{
            k -= c[a-1][b];
            b--;
            ans+= 'b';

        }
        //cout << c[a-1][b]<< endl;

    }

    cout << ans << endl;

    return 0;
}

#pragma GCC optimize("O3")
#include<bits/stdc++.h>
using namespace std;
using ll=long long;
using P=pair<ll,ll>;
template<class T> using V=vector<T>;
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
const ll inf=(1e18);
//const ll mod=998244353;
const ll mod=1000000007;
ll GCD(ll a,ll b) {return b ? GCD(b,a%b):a;}
ll LCM(ll c,ll d){return c/GCD(c,d)*d;}
struct __INIT{__INIT(){cin.tie(0);ios::sync_with_stdio(false);cout<<fixed<<setprecision(15);}} __init;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
template<class T>void debag(const vector<T> &a){cerr<<"debag :";for(auto v:a)cerr<<v<<" ";cerr<<"\n";}
template<class T>void print(const vector<T> &a){for(auto v:a)cout<<v<<" ";cout<<"\n";}
struct mint{
using ull=unsigned long long int;
ull v;
mint(ll vv=0){s(vv%mod+mod);}
mint& s(ull vv){
v=vv<mod?vv:vv-mod;
return *this;
}
//オーバーロード
mint operator-()const{return mint()-*this;}//mint型にキャスト
mint&operator+=(const mint&val){return s(v+val.v);}
mint&operator-=(const mint&val){return s(v+mod-val.v);}
mint&operator*=(const mint&val){
v=ull(v)*val.v%mod;
return *this;
}
mint&operator/=(const mint&val){return *this*=val.inv();}
mint operator+(const mint&val){return mint(*this)+=val;}
mint operator-(const mint&val){return mint(*this)-=val;}
mint operator*(const mint&val){return mint(*this)*=val;}
mint operator/(const mint&val){return mint(*this)/=val;}
mint pow(ll n)const{
mint res(1),x(*this);
while(n){
if(n&1)res*=x;
x*=x;
n>>=1ll;
}
return res;
}
mint inv()const{return pow(mod-2);}
//拡張ユークリッドの互除法
/* mint inv()const{
int x,y;
int g=extgcd(v,mod,x,y);
assert(g==1);
if(x<0)x+=mod;
return mint(x);
}*/
friend ostream& operator<<(ostream&os,const mint&val){
return os<<val.v;
}//出力
bool operator<(const mint&val)const{return v<val.v;}
bool operator==(const mint&val)const{return v==val.v;}
bool operator>(const mint&val)const{return v>val.v;}
};
const ll MAX = 2000010;//設定
mint fac[MAX], finv[MAX], inv[MAX];
// テーブルを作る前処理
void init(){
fac[0] = fac[1] = 1;
for(int i=1;i<MAX;i++)fac[i]=fac[i-1]*i;
finv[MAX-1]=fac[MAX-1].inv();
for(int i=MAX-2;i>=0;i--)finv[i]=finv[i+1]*(i+1);
for(int i=MAX-2;i>=1;i--)inv[i]=finv[i]+fac[i-1];
}
//階乗
mint factor(ll n,ll k){
if (n<k) return 0;
if (n<0 || k<0) return 0;
return fac[n]*finv[k];
}
// 二項係数計算
mint COM(int n, int k){
if (n < k) return 0;
if (n < 0 || k < 0) return 0;
return fac[n] * finv[k] * finv[n - k];
}
int main(){
    init();
    ll n,m,k;
    cin>>n>>m>>k;
    mint ALL=COM(n+m,n);
    if(n>k+m){
          cout<<0<<"\n";
          return 0;
    }
    cout<<ALL-COM(n+m,m+k+1)<<"\n";
}


#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<long long>A(N+1);
  for(int i=0; i<N; i++)cin >>A[i];

  long long ans=1;
  sort(A.begin(), A.end());
  for(int i=1; i<N+1; i++){
    ans*=A[i]-A[i-1]+1;
    ans%=1000000007;
  }

  cout << ans << endl;
}

#include <bits/stdc++.h>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define FOR(i,n,m) for(int i=(int)(n); i<=(int)(m); i++)
#define RFOR(i,n,m) for(int i=(int)(n); i>=(int)(m); i--)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define RITR(x,c) for(__typeof(c.rbegin()) x=c.rbegin();x!=c.rend();x++)
#define setp(n) fixed << setprecision(n)

template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }

#define ll long long
#define vll vector<ll>
#define vi vector<int>
#define pll pair<ll,ll>
#define pi pair<int,int>

#define all(a) (a.begin()),(a.end())
#define rall(a) (a.rbegin()),(a.rend())
#define fi first
#define se second
#define pb push_back
#define ins insert

#define debug(a) cerr<<(a)<<endl
#define dbrep(a,n) rep(_i,n) cerr<<(a[_i])<<" "; cerr<<endl
#define dbrep2(a,n,m) rep(_i,n){rep(_j,m) cerr<<(a[_i][_j])<<" "; cerr<<endl;}

using namespace std;

template<class A, class B>
ostream &operator<<(ostream &os, const pair<A,B> &p){return os<<"("<<p.fi<<","<<p.se<<")";}
template<class A, class B>
istream &operator>>(istream &is, pair<A,B> &p){return is>>p.fi>>p.se;}

template<class T>
vector<T> make_vec(size_t a){
	return vector<T>(a);
}
template<class T, class... Ts>
auto make_vec(size_t a, Ts... ts){
	return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}

//-------------------------------------------------
//--ModInt
//-------------------------------------------------
const uint_fast64_t MOD = 1e9+7;

class mint
{
private:
    using Value = uint_fast64_t;
    Value n;
public:
    mint():n(0){}
    mint(int_fast64_t _n):n(_n<0 ? MOD-(-_n)%MOD : _n%MOD){}
    mint(const mint &m):n(m.n){}

    friend ostream& operator<<(ostream &os, const mint &a){
		return os << a.n;
	}
    friend istream& operator>>(istream &is, mint &a){
        Value temp; is>>temp;
        a = mint(temp);
        return is;
    }

    mint& operator+=(const mint &m){n+=m.n; n=(n<MOD)?n:n-MOD; return *this;}
    mint& operator-=(const mint &m){n+=MOD-m.n; n=(n<MOD)?n:n-MOD; return *this;}
    mint& operator*=(const mint &m){n=n*m.n%MOD; return *this;}
    mint& operator/=(const mint &m){return *this*=m.inv();}
    mint& operator++(){return *this+=1;}
    mint& operator--(){return *this-=1;}

    mint operator+(const mint &m) const {return mint(*this)+=m;}
    mint operator-(const mint &m) const {return mint(*this)-=m;}
    mint operator*(const mint &m) const {return mint(*this)*=m;}
    mint operator/(const mint &m) const {return mint(*this)/=m;}
    mint operator++(int){mint t(*this); *this+=1; return t;}
    mint operator--(int){mint t(*this); *this-=1; return t;}

    bool operator==(const mint &m) const {return n==m.n;}
    bool operator!=(const mint &m) const {return n!=m.n;}
    mint operator-() const {return mint(MOD-n);}

    mint pow(Value b) const {
        mint ret(1), m(*this);
        while(b){
            if (b & 1) ret*=m;
            m*=m;
            b>>=1;
        }
        return ret;
    }

    mint inv() const {return pow(MOD-2);}
};

//-------------------------------------------------

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; cin>>N;
    vll a(N);
    rep(i,N) cin>>a[i];
    mint ans = 1;
    sort(all(a));
    ll cur = 0;
    rep(i,N){
        ans *= a[i]-cur+1;
        cur = a[i];
    }
    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
#define LL long long
using namespace std;
int Len,fact[400010];
char str[400010];
LL ans,KSM[5][400010],f[400010],g[400010];
LL crt_m[1010],crt_r[1010],crt_cnt;
inline int read()
{
	int x=0;bool w=0;char ch=0;
	while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
	while(isdigit(ch)){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return w?-x:x;
}
LL ksm(LL B,LL K,LL P,LL A=1)
{
	for(;K;K>>=1,B=B*B%P)
	if(K&1)A=A*B%P;
	return A%P;
}
LL G(LL N,LL P)
{
	if(g[N]!=-1)return g[N];
	return g[N]=(N<P?0:N/P+G(N/P,P));
}
LL F(LL N,LL P,LL PK,LL A=1)
{
	if(f[N]!=-1)return f[N];
	if(!N)return f[N]=1;
	return f[N]=KSM[fact[PK]][N/PK]*fact[N%PK]%PK*F(N/P,P,PK)%PK;
}
LL C_pk(LL N,LL M,LL P,LL PK)
{
	LL fz=F(N,P,PK),fm1=F(M,P,PK),fm2=F(N-M,P,PK);
	return fz*fm1%PK*fm2%PK*(G(N,P)-G(M,P)-G(N-M,P)==0)%PK;
}
int main()
{
	fact[0]=1;
	memset(g,-1,sizeof g);
	memset(f,-1,sizeof f);
	for(int i=1;i<=3;i++)
	if(i%3)fact[i]=fact[i-1]*i%3;
	else fact[i]=fact[i-1];
	KSM[0][0]=KSM[1][0]=KSM[2][0]=1;
	for(int i=1;i<=400000;i++){
		KSM[1][i]=1;
		KSM[2][i]=KSM[2][i-1]*2%3;
	}
	Len=read();
	scanf("%s",str+1);
	for(int i=1;i<=Len;i++)
	if(str[i]=='B')ans=(ans+C_pk(Len-1,i-1,3,3)*0)%3;
	else if(str[i]=='W')ans=(ans+C_pk(Len-1,i-1,3,3)*1)%3;
	else ans=(ans+C_pk(Len-1,i-1,3,3)*2)%3;
	if(Len%2==0)ans=(-ans+3)%3;
	if(ans==0)printf("B\n");
	else if(ans==1)printf("W\n");
	else printf("R\n");
	return 0;
}

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
int comb[10][10];
int lucas (int n, int k) {
  int res = 1;
  while (n > 0) {
    res *= comb[n % 3][k % 3];
    n /= 3;
    k /= 3;
    res %= 3;
  }
  return res;
}
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  comb[0][0] = 1;
  for (int i = 1; i < 4; i++) {
    for (int j = 0; j <= i; j++) {
      comb[i][j] = comb[i - 1][j];
      if (j > 0) comb[i][j] += comb[i - 1][j - 1];
    }
  }
  int n;
  cin >> n;
  string s;
  cin >> s;
  int res = 0;
  for (int i = 0; i < n; i++) {
    int c = 0;
    if (s[i] == 'R') c = 0;
    if (s[i] == 'W') c = 1;
    if (s[i] == 'B') c = 2;
    res += c * lucas(n - 1, i);
    res %= 3;
  }
  if (n % 2 == 0) res = (3 - res) % 3;
  cerr << res << endl;
  if (res == 0) cout << 'R';
  if (res == 1) cout << 'W';
  if (res == 2) cout << 'B';
  cout << endl;
  return 0;
}


#include <bits/stdc++.h>
#include <math.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for(int i = 0; i < (int)(n); i ++)

vector <int> dy = {0, 1, 0, -1};
vector <int> dx = {1, 0, -1, 0};

const int INF = 1000000000;
const ll INFLL = 100000000000000000;

int main(){
    int n, m; cin >> n >> m;
    vector <ll> a(n);
    rep(i, n) cin >> a.at(i);
    vector <vector<int>> G(n);
    rep(i, m){
        int x, y; cin >> x >> y;
        x--; y--;
        G.at(x).push_back(y);
    }

    vector <ll> dp(n, -INFLL);
    ll ans = -INFLL;
    vector <int> seen(n, -1);
    for(int i = n - 1; i >= 0; i --){
        seen.at(i) = 1;
        auto children = G.at(i);
        //if(children.size() == 0) continue;
        //sort(children.rbegin(), children.rend());
        // auto great_child = children.at(0);
        // dp.at(i) = max(dp.at(i), max(dp.at(great_child) + a.at(great_child) - a.at(i), a.at(great_child) - a.at(i)));
        for(auto child: children){
            dp.at(i) = max(dp.at(i), max(dp.at(child) + a.at(child) - a.at(i), a.at(child) - a.at(i)));
        }
        ans = max(ans, dp.at(i));
        //cout << dp.at(i) << endl;
    }

    cout << ans << endl;

}

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int maxn = 2050,inf = 1e9;
int n,m,x,y,z,last[maxn],ms[maxn],vis[maxn],dis[maxn],g[maxn][maxn];
struct Edge{
	int v,w,nxt;
}e[maxn];
priority_queue <pair<int,int> > q;
int read(){
	int x = 0;
	char c = getchar();
	while(c < '0' || c > '9') c = getchar();
	while(c >= '0' && c <= '9') x = x * 10 + (c ^ 48),c = getchar();
	return x;
}
inline void insert(int u,int v,int w){
	static int cnt = 0;
	e[++cnt] = {v,w,last[u]},last[u] = cnt;
}
void Dijkstra(int S){
	for(int i = 1; i <= n; i ++) vis[i] = 0,dis[i] = inf;
	dis[S] = 0,q.push({0,S});
	while(!q.empty()){
		int u = q.top().second;
		q.pop();
		if(vis[u]) continue;
		vis[u] = 1;
		for(int i = last[u]; i; i = e[i].nxt){
			int v = e[i].v;
			if(dis[u] + e[i].w < dis[v]){
				dis[v] = dis[u] + e[i].w;
				q.push({-dis[v],v});
			}
		}
	}
	for(int i = 1; i <= n; i ++) g[S][i] = dis[i];
	g[S][S] = inf;
}
int main(){
	n = read(),m =  read();
	for(int i = 1; i <= n; i ++) ms[i] = inf;
	for(int i = 1; i <= m; i ++){
		x = read(),y = read(),z = read();
		if(x == y){
			ms[x] = min(ms[x],z);
			continue;
		}
		insert(x,y,z);
	}
	for(int i = 1; i <= n; i ++) Dijkstra(i);
	for(int i = 1; i <= n; i ++){
		int mn = inf;
		for(int j = 1; j <= n; j ++) mn = min(mn,g[i][j] + g[j][i]);
		printf("%d\n",mn == inf && ms[i] == inf ? -1 : min(mn,ms[i]));
	}
	return 0;
}


#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
typedef long long ll;

using namespace std;

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

    map<int, int> m;
    rep(i, n) {
        int a;
        cin >> a;
        if (m.count(a)) {
            m[a] = m[a] + 1;
        } else {
            m[a] = 1;
        }
    }

    // (i + j)Cj = dp[i][j]
    ll dp[300010][10] = {0};

    dp[0][0] = 1;

    for (int i = 0; i <= 300000; i++) {
        for (int j = 0; j <= 2; j++) {
            if (i > 0) dp[i][j] += dp[i - 1][j];
            if (j > 0) dp[i][j] += dp[i][j - 1];
        }
    }

    ll ans = dp[n - 2][2];
    for (auto v : m) {
        if (v.second > 1) ans -= dp[v.second - 2][2];
    }

    cout << ans << endl;

    return 0;
}

#include <bits/stdc++.h>
using namespace std;

// template {{{
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;

#define  range(i, l, r) for (i64 i = (i64)(l); i < (i64)(r); (i) += 1)
#define rrange(i, l, r) for (i64 i = (i64)(r) - 1; i >= (i64)(l); (i) -= 1)

#define  whole(f, x, ...) ([&](decltype((x)) container) { return (f)(  begin(container),  end(container), ## __VA_ARGS__); })(x)
#define rwhole(f, x, ...) ([&](decltype((x)) container) { return (f)( rbegin(container), rend(container), ## __VA_ARGS__); })(x)

#define debug(x) cerr << "(" << __LINE__ << ")" << #x << ": " << (x) << endl

constexpr i32 inf   = 1001001001;
constexpr i64 infll = 1001001001001001001ll;

constexpr i32 dx[] = {0, -1, 1, 0, -1, 1, -1, 1};
constexpr i32 dy[] = {-1, 0, 0, 1, -1, -1, 1, 1};

struct IoSetup { IoSetup(i32 x = 15){ cin.tie(0); ios::sync_with_stdio(0); cout << fixed << setprecision(x); cerr << fixed << setprecision(x); } } iosetup;

template <typename T = i64> T input() { T x; cin >> x; return x; }

template <typename T> ostream &operator<<(ostream &os, vector<T> &v) { range(i, 0, v.size()) { os << v[i] << (i + 1 != (int)v.size() ? " " : ""); } return os; }
template <typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &in : v) is >> in; return is; }

template <typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p) { os << "(" << p.first << ", " << p.second << ")"; return os; }
template <typename T1, typename T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; }

template <typename T> vector<T> make_vector(size_t a, T b) { return vector<T>(a, b); }
template <typename... Ts> auto make_vector(size_t a, Ts... ts) { return vector<decltype(make_vector(ts...))>(a, make_vector(ts...)); }

template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) { return a < b && (a = b, true); }
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) { return a > b && (a = b, true); }
// }}}

void solve() {
  int n = input();

  map<int, int> cnt;
  i64 ans = 0;
  range(i, 0, n) {
    int a = input();
    ans += i - cnt[a];
    cnt[a]++;
  }

  cout << ans << endl;
}

signed main() {
  solve();
}


#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long LL;

LL mod = 998244353;

LL N , M , K;

LL qpow(LL a , LL b) {
	LL res = 1;
	while(b) {
		if(b & 1) res = res * a % mod;
		a = a * a % mod;
		b >>= 1;
	}
	return res;
}

int main() {
	scanf("%lld %lld %lld" , &N , &M , &K);
	if(N == 1 || M == 1) {
		printf("%lld" , qpow(K , max(N , M)));
		return 0;
	}
	LL ans = 0;
	for (int i = 1; i <= K; ++i) {
		ans = (ans + (qpow(i , N) - qpow(i - 1 , N) + mod) % mod * qpow(K - i + 1 , M) % mod) % mod;
	}
	printf("%lld" , ans);
	return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define rep(i,n) for (int i=1;i <=int(n);i++)
#define rrep(i,n) for(int i= int(n);i>=1;i--)
#define ffor(i,n) for (int i=0;i <int(n);i++)

#define pii pair<int,int>
#define type pair<pair<ll,ll>,pair<ll,ll>>

int a[1001],b[1001];
int main(){
     ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     int n,d,h; cin >> n >> d >> h;
     rep(i,n){
          cin >> a[i] >> b[i];
     }

     double ans = 0;
     rep(i,n){
          double res = (h - b[i]) * double(d)/(d-a[i]);
          ans = max (ans,h-res);
     }

     cout << ans << '\n';

return 0;
}

 //check for bounds


#include<iostream>
#include<vector>
#include<bitset>
#include<algorithm>
using namespace std;
unsigned xorshift()
{
	static unsigned y=2463534242;
	y^=y<<13;
	y^=y>>17;
	y^=y<<5;
	return y;
}
const int N=20;
int M;
string S[800];
string outH[N],outW[N];
int G[800][800];
using bit=bitset<800>;
bit rest;
bit can[N+1][800];
int cnt[800];
int main()
{
	cin>>M>>M;
	vector<int>idx(M);
	for(int i=0;i<M;i++)
	{
		cin>>S[i];
		rest[i]=1;
		cnt[i]=1;
		idx[i]=i;
	}
	sort(idx.begin(),idx.end(),[](int i,int j){return S[i].size()<S[j].size();});
	for(int i:idx)for(int j:idx)if(i!=j&&rest[j])
	{
		for(int k=1;k<=S[i].size();k++)
		{
			int len=S[i].size()-k;
			if(S[j].size()<len)len=S[j].size();
			if(S[i].substr(k,len)==S[j].substr(0,len))
			{
				G[i][j]=S[j].size()-len;
				if(G[i][j]==0)
				{
					rest[j]=0;
					cnt[i]+=cnt[j];
					cnt[j]=0;
				}
				break;
			}
		}
	}
	for(int tm=0;tm<N;tm++)
	{
		string now="";
		if(rest.any())
		{
			vector<vector<int> >dist(N+1,vector<int>(M,0));
			vector<vector<int> >pr(N+1,vector<int>(M,-1));
			for(int v=0;v<M;v++)if(rest[v])
			{
				for(int u=S[v].size();u<=N;u++)
				{
					dist[u][v]=cnt[v];
					can[u][v]=rest;
					can[u][v][v]=0;
				}
			}
			for(int u=0;u<N;u++)for(int v=0;v<M;v++)if(dist[u][v])
			{
				for(int w=0;w<M;w++)if(can[u][v][w])
				{
					int nu=u+G[v][w];
					if(nu<=N&&dist[nu][w]<dist[u][v]+cnt[w])
					{
						dist[nu][w]=dist[u][v]+cnt[w];
						pr[nu][w]=v;
						can[nu][w]=can[u][v];
						can[nu][w][w]=0;
					}
				}
			}
			int go=-1;
			for(int v=0;v<M;v++)if(go==-1||dist[N][v]>dist[N][go])go=v;
			vector<int>vs;
			{
				int nu=N;
				while(true)
				{
					vs.push_back(go);
					int ngo=pr[nu][go];
					if(ngo<0)break;
					nu-=G[ngo][go];
					go=ngo;
				}
			}
			now=S[go];
			for(int i=vs.size()-1;i--;)
			{
				int d=G[go][vs[i]];
				go=vs[i];
				now+=S[go].substr(S[go].size()-d);
			}
		}
		while(now.size()<N)now+='.';
		cout<<now<<endl;
		outW[tm]=now+now;
		for(int j=0;j<N;j++)outH[j]+=now[j];
		for(int i=0;i<M;i++)if(rest[i])
		{
			for(int k=0;k<N;k++)if(outW[tm].substr(k,S[i].size())==S[i])
			{
				rest[i]=0;
				break;
			}
			if(rest[i]&&tm+1>=S[i].size())
			{
				for(int j=0;j<N;j++)if(outH[j].substr(tm+1-S[i].size())==S[i])
				{
					rest[i]=0;
					break;
				}
			}
		}
	}
}

#include <iostream>
#include <cmath>
#include <string>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#include <cstdint>
#include <cstdio>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <deque>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
#include <time.h>

#define rep(i, n) for(int i = 0; i < n; i++)
#define per(i, n) for(int i = n - 1; i >= 0; i--)
using ll = long long;
#define vi vector<int>
#define vvi vector<vi>
#define vl vector<ll>
#define vd vector<double>
#define vvd vector<vd>
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdi pair<double, int>
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
using namespace std;
constexpr int n = 20;
int m;
vector<string> s;
vector<deque<string>> ss(n + 1);
vvi board(n, vi(n));
void tekitou_sort(){
	rep(i, n){
		int nokori = n;
		while(true){
			bool found = false;
			for(int j = nokori; j >= 0; j--) if(!ss[j].empty()){
				found = true;
				rep(k, j) board[i][n-nokori+k] = ss[j][0][k] - 'A';
				nokori -= j;
				ss[j].pop_front();
				break;
			}
			if(!found) break;
		}
	}
}
void inputs(){
	cin >> m >> m;
	s.resize(m);
	rep(i, m) cin >> s[i];
}
int main(){
	srand(time(NULL));
	inputs();
	rep(i, m) ss[s[i].size()].push_back(s[i]);

	rep(i, n) rep(j, n) board[i][j] = rand() % 8;
	tekitou_sort();

	rep(i, n){
		rep(j, s[i].size()) board[i][j] = s[i][j] - 'A';
	}
	rep(i, n){
		rep(j, n) printf("%c", board[i][j] + 'A');
		printf("\n");
	}
}


#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int a, b, c;
  cin >> a >> b >> c;
  cout << 7 * 3 - (a + b + c) << endl;
}

#include <bits/stdc++.h>

using namespace std;

int f(int x){
  return 7 - x;
}

int main(){

  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int a, b, c;
  cin >> a >> b >> c;

  cout << f(a) + f(b) + f(c) << '\n';

  return 0;
}


#include<bits/stdc++.h>
#define ll long long int
#define pll pair<ll,ll>
#define vpll vector< pll >
#define mpll map<ll,ll>
#define MOD 1000000007
#define all(v) v.begin(),v.end()
#define s(v) v.size()
#define test ll t;cin>>t;while(t--)
#define vec vector<ll>
#define read0(v,n) for(int i=0;i<n;i++)cin>>v[i];
#define read1(v,n) for(int i=1;i<=n;i++)cin>>v[i];
#define trav(a,x) for (auto& a: x)
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);
#define cut(x) {cout<<x;return 0;}
#define print(x) {cout<<x<<nl;continue;}
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORB(i,a,b) for(int i=a;i>=b;i--)
#define err1(a) {cout<<#a<<' '<<a<<nl;}
#define err2(a,b) {cout<<#a<<' '<<a<<' '<<#b<<' '<<b<<nl;}

#define mp make_pair
#define pb push_back
#define eb emplace_back
#define f first
#define sc second
#define lb lower_bound
#define ub upper_bound
#define nl '\n'
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace __gnu_pbds;
using namespace std;
#define oset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
ll gcd(ll a, ll b)
{
	if (b==0)return a;
	return gcd(b, a % b);
}
ll lcm(ll a,ll b)
{
    return (a*b)/gcd(a,b);
}
ll bpow(ll a, ll b)
{
	ll ans=1;
	while(b)
	{
		if(b&1)
			ans=(ans*a)%MOD;
		b/=2;
		a=(a*a)%MOD;
	}
	return ans;
}
ll dp[26][200005];
int main()
{
	//fast
	string s;
	cin>>s;
	int n=s(s);
	s='#'+s;
	FOR(i,1,n)
	{
	    int x=s[i]-'a';
	    dp[x][i]++;
	}
	FOR(i,0,25)
	{
	    FOR(j,1,n)
	    {
	        dp[i][j]+=dp[i][j-1];
	    }
	}
    //FOR(i,0,25)FOR(j,1,n)cout<<dp[i][j]<<(j==n?nl:' ');
	vector<pair<char,int>> v;
	FORB(i,n,2)
	{
	    if(s[i]==s[i-1])
        {
            v.eb(s[i],i);
        }
	}
	if(s(v)==0)cut(0)
    char x='?';ll r=n;
    ll ans=0;
   // trav(it,v)cout<<it.f<<' '<<it.sc<<nl;
    trav(it,v)
    {
        if(it.f!=x)
        {
            int y=it.sc;
            ans+=(n-it.sc)-(dp[it.f-'a'][r]-dp[it.f-'a'][y]);
            r=y-1;
            x=it.f;
        }
        else
        {
            int y=it.sc;
            ans+=(r-y)-(dp[it.f-'a'][r]-dp[it.f-'a'][y]);

            r=y-1;
        }
        //err2(x,r)
        //err1(ans)
    }
    cut(ans)

}

#include <bits/stdc++.h>
// clang-format off
using namespace std; using ll = long long; using ull = unsigned long long; using pll = pair<ll,ll>; const ll INF = 4e18;
void print0() {}
template<typename Head,typename... Tail> void print0(Head head,Tail... tail){cout<<fixed<<setprecision(15)<<head;print0(tail...);}
void print() { print0("\n"); }
template<typename Head,typename... Tail>void print(Head head,Tail... tail){print0(head);if(sizeof...(Tail)>0)print0(" ");print(tail...);}
// clang-format on

int main() {
    string s;
    cin >> s;
    ll n = s.size();
    vector<vector<ll>> rightcnt(n, vector<ll>(27, 0));
    // for (ll i = n - 1; i >= 0; i--) {
    //     for (ll c = 0; c < 27; c++) {
    //         if (i < n - 1) rightcnt[i][c] = rightcnt[i + 1][c];
    //         ll cur = s[i] - 'a';
    //         rightcnt[i][cur]++;
    //     }
    // }
    ll ans = 0;
    //print(n);
    for (ll i = n - 1; i >= 0; i--) {
        ll cur = s[i] - 'a';
        for (ll c = 0; c < 27; c++) {
            if (i + 1 < n) rightcnt[i][c] = rightcnt[i + 1][c];
            //  print(i, c, rightcnt[i][c]);
        }
        rightcnt[i][cur]++;
        if (i + 2 < n && s[i] == s[i + 1] && s[i] != s[i + 2]) {
            for (ll c = 0; c < 27; c++) {
                if (c == cur) {
                    rightcnt[i][c] = (n - i);
                } else {
                    ans += rightcnt[i][c];
                    rightcnt[i][c] = 0;
                }
            }
            s[i + 1] = s[i];
            s[i + 2] = s[i];
        }
    }
    print(ans);
}


#include <algorithm>
#include <iostream>
#include <vector>

using std::cin;
using std::cout;
using std::min;
using std::sort;
using std::vector;

struct block {
    int x, y;
} blocks[400000];

int up_most[200001];

vector<vector<int>> sum;

void set(int place) {
    if (!sum[0][place]) {
        for (int bit = 0; ; ++bit) {
            ++sum[bit][place];
            place >>= 1;
            if (sum[bit].size() == 1) {
                break;
            }
        }
    }
}

int get(int left, int right) {
    int bit = 0, ret = 0;
    while (left < right) {
        if (left & 1) {
            ret += sum[bit][left];
            left = left / 2 + 1;
        } else {
            left >>= 1;
        }
        if (right & 1) {
            right >>= 1;
        } else {
            ret += sum[bit][right];
            right = right / 2 - 1;
        }
        ++bit;
    }
    return left == right ? ret + sum[bit][left] : ret;
}

int main() {
    std::ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int h, w, m;
    long long ans = 0;
    cin >> h >> w >> m;
    for (int i = w; i; --i) {
        up_most[i] = h + 1;
    }
    for (int i = 0; i < m; ++i) {
        cin >> blocks[i].x >> blocks[i].y;
        up_most[blocks[i].y] = min(up_most[blocks[i].y], blocks[i].x);
    }
    for (int i = w + 2; ; i = i + 1 >> 1) {
        sum.push_back(vector<int>(i, 0));
        if (i == 1) {
            break;
        }
    }
    for (int i = 1; i <= w; ++i) {
        if (up_most[i] == 1) {
            while (i < w) {
                set(++i);
            }
            break;
        } else {
            ans += up_most[i] - 1;
        }
    }
    for (int i = 0; i < h; ++i) {
        blocks[m + i] = {i + 1, w + 1};
    }
    sort(blocks, blocks + m + h, [](block a, block b) {
        return a.x == b.x ? a.y < b.y : a.x < b.x;
    });
    for (int x = 2, i = 0; x <= h; ++x) {
        while (blocks[i].x < x) {
            set(blocks[i++].y);
        }
        if (blocks[i].y == 1) {
            break;
        } else {
            ans += get(1, blocks[i].y - 1);
        }
    }
    cout << ans;
    return 0;
}

#include<bits/stdc++.h>
#define int long long
using namespace std;
vector<int>a[3];
int n,mp[200],x,y,ok,ans=LLONG_MAX,m1=LLONG_MAX,m2=LLONG_MAX;
signed main()
{
	ios::sync_with_stdio(0);
	mp['R']=0,mp['G']=1,mp['B']=2;
	cin>>n;
	n<<=1;
	for(int i=1;i<=n;i++)
	{
		int x;
		char t;
		cin>>x>>t;
		a[mp[t]].push_back(x);
	}
	ok=1;
	for(int i=0;i<3;i++)
	{
		sort(a[i].begin(),a[i].end());
		if(a[i].size()&1)
			ok=0;
	}
	if(ok)
	{
		cout<<0<<endl;
		return 0;
	}
	for(int i=0;i<3;i++)
		if(a[i].size()&1)
			x=i;
	for(int i=2;i>=0;i--)
		if(a[i].size()&1)
			y=i;
	int z=3-x-y;
	for(int i=0;i<a[x].size();i++)
	{
		int v=a[x][i];
		int p=lower_bound(a[y].begin(),a[y].end(),v)-a[y].begin();
		if(p!=a[y].size()) ans=min(ans,abs(v-a[y][p]));
		if(p) ans=min(ans,abs(v-a[y][p-1]));
	}
	for(int i=0;i<a[x].size();i++)
	{
		int v=a[x][i];
		int p=lower_bound(a[z].begin(),a[z].end(),v)-a[z].begin();
		if(p!=a[z].size()) m1=min(m1,abs(v-a[z][p]));
		if(p) m1=min(m1,abs(v-a[z][p-1]));
	}
	for(int i=0;i<a[y].size();i++)
	{
		int v=a[y][i];
		int p=lower_bound(a[z].begin(),a[z].end(),v)-a[z].begin();
		if(p!=a[z].size()) m2=min(m2,abs(v-a[z][p]));
		if(p) m2=min(m2,abs(v-a[z][p-1]));
	}
	if(m1<1e18)
		ans=min(ans,m1+m2);
	cout<<ans<<endl;
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int a,b,w,minn = 1e9,maxx = -1e9;
bool ok = false;
int main()
{
	cin>>a>>b>>w;w*=1000;
	for(int i=1;;i++)
	{

		if (a*i<=w && w<=b*i && !ok){
			minn = i;ok = true;
		}
		if (a*i>w && ok){
			maxx = i-1;break;
		}
		if (a*i>1000000)break;
	}
	if (!ok){
		printf("UNSATISFIABLE");return 0;
	}

	cout<<minn<<" "<<maxx;
	return 0;
}

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define getbit(n, i) (((n) & (1LL << (i))) != 0)
#define setbit0(n, i) ((n) & (~(1LL << (i))))
#define setbit1(n, i) ((n) | (1LL << (i)))
#define togglebit(n, i) ((n) ^ (1LL << (i)))
#define lastone(n) ((n) & (-(n)))
char gap = 32;
#define ll long long
#define lll __int128_t
#define pb push_back
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll hashPrime = 1610612741;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    ll i,j,k,l,n,m;
    cin>>n>>m>>l;
    l *= 1000;
    ll mini = 0;
    ll maxi = 0;
    int flag = 0;
    for(i = 1;i<=l;i++){
        long double temp = (long double)((long double)l/(long double)i);
        if(temp>=n and temp<=m){
            if(flag==0){
                flag = 1;
                mini = i;
                maxi = i;
            }
            else{
                mini = min(mini , i);
                maxi = max(maxi , i);
            }
        }
    }
    if(flag==0){
        cout<<"UNSATISFIABLE\n";
    }
    else{
        cout<<mini<<" "<<maxi<<"\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main(){
    int t;
    cin>>t;
    vector<pair<int,int>> m(t);
    rep(i,t){
        cin>>m.at(i).first>>m.at(i).second;
    }
    vector<int64_t> nums(t);
    rep(i,t){
        int64_t min_c=m.at(i).first;
        int64_t max_c=m.at(i).second;
        if (max_c-min_c>=min_c){
            int64_t sum=(1+(max_c-2*min_c+1))*(max_c-2*min_c+1)/2;
            nums.at(i)=sum;
        }else nums.at(i)=0;
    }
    rep(i,t) cout<<nums[i]<<endl;

}

#include<bits/stdc++.h>
using namespace std;
int read(){
	int x=0,f=1;char ch=getchar();
	while(ch>'9'||ch<'0'){if(ch=='-')f=-1;ch=getchar();}
	while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x*f;
}
int tests,n,a,b;
int main(){
	a=read(),b=read();
	printf("%d\n",2*a-b+100);
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define flush cout.flush

using ll = int;
using ull = unsigned long long;
using ld = long double;
using pl = pair<ll, ll>;
const ll INF = 1e9 + 7;
const ll mod = 1e9 + 7;
const ll mod2 = 998244353;
const ld eps = 1e-9;
const ld PI = acos(-1);

const ll LOG = 13;

ll bit[LOG];

int main() {
    ios::sync_with_stdio(false);
    cin.tie(NULL);
    for (ll i = 0; i < LOG; ++i) {
        bit[i] = (1 << i);
    }
    ll n, m;
    cin >> n >> m;
    vector<ll> good(m + 1, 0);
    vector<ll> bad(m + 1, 0);
    good[0] = 1;
    for (ll p = 0; p < LOG; ++p) {
        bad.clear();
        bad.assign(m + 1, 0);
        for (ll i = 0; i < n; ++i) {
            vector<ll> new_good(m + 1, 0);
            vector<ll> new_bad(m + 1, 0);
            for (ll j = 0; j <= m; ++j) {
                new_good[j] = good[j];
                new_bad[j] = bad[j];
                if (j - bit[p] >= 0) {
                    new_good[j] += bad[j - bit[p]];
                    new_bad[j] += good[j - bit[p]];
                }
                new_good[j] %= mod2;
                new_bad[j] %= mod2;
            }
            good = new_good;
            bad = new_bad;
        }
    }
    cout << good[m] << "\n";
    return 0;
}

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
	bool flag=1;
	int x=0;
	char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-')
			flag=0;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return (flag?x:~(x-1));
}
int n,m,c[5001][5001],f[5001];
const int mod=998244353;
signed main()
{
	n=read();
	m=read();
	for(int i=0;i<=n;i++)
	{
		c[i][0]=1;
		for(int l=1;l<=i;l++)
			c[i][l]=(c[i-1][l-1]+c[i-1][l])%mod;
	}
	f[0]=1;
	for(int i=0;i<=11;i++)
	{
		int t=(1<<i)<<1;
		for(int l=m;l>=t;l--)
			for(int j=t,k=2;j<=l;j+=t,k+=2)
				(f[l]+=f[l-j]*c[n][k]%mod)%=mod;
	}
	cout<<f[m];
	return 0;
}


#pragma GCC optimise ("O1")
#include<bits/stdc++.h>
using namespace std;
#define ll           long long
#define rep(i,j,n)   for(ll i=j;i<n;i++)
#define _rep(i,n)   for(ll i=n-1;i>=0;i--)
#define scn(a)       scanf("%lld",&a)
#define scns(a,b)    scanf("%lld %lld",&a,&b)
#define print(a)     printf("%lld ",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 srt(v)       sort(v.begin(),v.end())
#define read(a)       for(auto &it:a) cin>>it;
#define write(a)      for(auto it:a) cout<<it<<" ";
#define yeS(GOOD)      GOOD ? cout<<"YES\n" : cout<<"NO\n"
#define mem(a,b)     memset(a,b,sizeof(a))
#define nl           printf("\n")
#define inf          1e9
#define zer          INT_MIN
#define fast         ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
const int N=2e5 + 5;

ll n;

int main()
{
  // fast;
  int t1=1;
  // cin>>t1;
  while(t1--)
  {
    ll n,x; scns(n,x);
    string s; cin>>s;

    for(auto it:s)
    {
      if(it=='o')
      ++x;
      else
      {
        x=max(0LL,--x);
      }
    }
     cout<<x;
  }
}

#include<bits/stdc++.h>
using namespace std;
int main()
{
	long int n,x;
	cin>>n>>x;
	string s;
	cin>>s;
	for(int i=0;i<s.length();i++)
	{
		if(s[i]=='o')
		x++;
		else if(s[i]=='x')
		{
			if(x==0)
			{
				continue;
			}
			else
			x--;
		}
	}
	cout<<x<<endl;
}


#include <iostream>
#include <vector>
#define MSEED 1000000007
#define MCUT(num) ((num)>=0?((num)%MSEED):((MSEED+(num))%MSEED))

using namespace std;

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

	long long num[n];

	for (int i = 0; i < n; i++) {
		cin >> num[i];
	}

	if (n == 1)
		cout << num[0] << endl;
	else {
		long long pos, neg, pos_prev, neg_prev;
		long long tp, tn;

		// loop 0
		pos = num[0];
		neg = 0;
		tp = 1;

		// loop 1
		pos_prev = pos;
		neg_prev = neg;
		pos = MCUT(pos_prev + num[1]);
		neg = MCUT(pos_prev - num[1]);
		tp = tp;
		tn = tp;

		// loop 2+
		for (int i = 2; i < n; i++) {
			pos_prev = pos;
			neg_prev = neg;
			pos = MCUT(MCUT(pos_prev + num[i] * tp) + MCUT(neg_prev + num[i] * tn));
			neg = MCUT(MCUT(pos_prev - num[i] * tp));
			long long tntmp = tn;
			tn = MCUT(tp);
			tp = MCUT(tp + tntmp);
		}

		cout << MCUT(pos + neg) << endl;
	}

	return 0;
}

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
#include <bits/stdc++.h>
#define mem(a,x) memset(a,x,sizeof(a))
#define gi(x) scanf("%d",&x)
#define gi2(x,y) scanf("%d%d",&x,&y)
#define gll(x) scanf("%lld",&x)
#define gll2(x,y) scanf("%lld%lld",&x,&y)
using namespace std;
const double eps=1e-8;
typedef long long ll;
const int MAXN=100005;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
using namespace std;
ll dp[MAXN][2];
ll a[MAXN];
int f[MAXN];
int main() {

	int n;
	gi(n);
	for(int i=1;i<=n;i++)gll(a[i]);

	dp[1][0]=1;
	for(int i=2;i<=n;i++){
		dp[i][0]=(dp[i-1][1]+dp[i-1][0])%mod;
		dp[i][1]=(dp[i-1][0])%mod;
	}
	ll ans=0;
	ans=(dp[n][1]+dp[n][0])*a[1]%mod;
	for(int i=2;i<=n;i++){
		int l=i-1;
		ll num=(dp[l][0]+dp[l][1])%mod;
		int r=n-i+1;
		num=(num*(dp[r][0]+dp[r][1]))%mod;
		ans=(ans+num*a[i])%mod;

		num=dp[l][0]*dp[r][0]%mod;
		ans=((ans-num*a[i])%mod+mod)%mod;
	}
	cout<<ans<<endl;

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define e1 first
#define e2 second
#define pb push_back
#define mp make_pair
#define boost {ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); }
#define eb emplace_back
#define OUT(x) {cout << x; exit(0); }
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define scanf(...) scanf(__VA_ARGS__)?:0
typedef long long int ll;
typedef unsigned long long ull;
typedef pair <int, int> PII;
typedef pair <ll, ll> PLL;
typedef pair <PLL, int> PLLI;
typedef pair <PII, PII> PP;
typedef pair <PII, int> PPI;
typedef pair <int, PII> PIP;
typedef pair <ll, int> PLI;
typedef unsigned int ui;
const int inf = 1e9+9;
const ll MOD = 1e9+696969;
#ifdef DEBUG
template<class T> int size(T &&x) {
	return int(x.size());
}
template<class A, class B> ostream& operator<<(ostream &out, const pair<A, B> &p) {
	return out << '(' << p.first << ", " << p.second << ')';
}
template<class T> auto operator<<(ostream &out, T &&x) -> decltype(x.begin(), out) {
	out << '{';
	for(auto it = x.begin(); it != x.end(); ++it)
		out << *it << (it == prev(x.end()) ? "" : ", ");
	return out << '}';
}
void dump() {}
template<class T, class... Args> void dump(T &&x, Args... args) {
	cerr << x << ";  ";
	dump(args...);
}
#endif
#ifdef DEBUG
  struct Nl{~Nl(){cerr << '\n';}};
# define debug(x...) cerr << (strcmp(#x, "") ? #x ":  " : ""), dump(x), Nl(), cerr << ""
#else
# define debug(...) 0 && cerr
#endif
mt19937_64 rng(0);
int random(int l, int r) {
	return uniform_int_distribution<int>(l, r)(rng);
}

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define trav(a, x) for(auto& a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi;
const LL INF = 1e18;
const int maxn = 300100;
const int mod = 998244353;
int dr[maxn];

void add(int p, int v) {
	for (; p < maxn; p += p & (-p)) dr[p] += v;
}

int get(int p) {
	int res = 0;
	for (; p > 0; p -= p & (-p)) res += dr[p];
	return res;
}

ll inv(vector <int> &perm) {
	int n = perm.size();
	ll res = 0;
	for (int i=n-1; i>=0; --i) {
		res += get(perm[i]);
		add(perm[i], 1);
	}

	return res;
}

void solve() {
	int n;
	cin >> n;
	set <PII> q;
	FOR(i, 0, n-1) {
		int a;
		cin >> a;
		q.insert({a + i, i + 1});
	}

	vector <int> perm;
	vector <int> B(n + 1);
	FOR(i, 1, n) {
		cin >> B[i];
	}

	FOR(i, 1, n) {
		int a = B[i];
		int co = a + i - 1;
		set <PII> :: iterator szukane = q.lower_bound(make_pair(co, -1));
		if (szukane == q.end() || szukane->first != co) {
			cout << "-1\n";
			return;
		}

		perm.push_back(szukane -> e2);
		q.erase(szukane);
	}
	debug(perm);
	cout << inv(perm) << "\n";
}

int main() {
	boost;
	int tests;
	tests = 1;
	//cin >> tests;
	while (tests--) {
		solve();
	}
}

#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int a[N],b[N],bit[N],id[N],x;
using ll = long long;
map<int,queue<int>> p;
int main(void){
	int n;
	 ios::sync_with_stdio(false);
    cin.tie(nullptr);
	cin >> n;
	for(int i = 1; i <= n; i++){
		cin >> x;
		p[x + i].push(i);
	}
	for(int i = 1; i <= n; i++){
		cin >> x;
		x += i;
		if(p[x].empty())return puts("-1"),0;
		id[p[x].front()]=i,p[x].pop();
	}
	ll ans = 0;
	for(int i = 1; i <= n; i++){
		int Now = id[i];
		int y = Now;
		for(;y <= n; y += (y & (-y))) ans += bit[y];
		y = Now;
		for(;y; y -= (y & (-y))) bit[y]++;
	}
	cout << ans <<"\n";
}


#include <iostream>
#include <vector>

using namespace std;

bool is_ok (int x) {
  if (x % 2 != 0) return false;
  if (x % 3 == 0) return true;
  if (x % 5 == 0) return true;
  if (x % 7 == 0) return true;
  if (x % 11 == 0) return true;
  return false;
}

int main () {
  vector<int> ans;
  ans.push_back(3 * 5 * 7 * 11);

  int n;
  cin >> n;

  for (int i = 1; i <= 10000 && (int) ans.size() < n; i++) {
    if (is_ok(i)) {
      ans.push_back(i);
    }
  }

  for (int i = 0; i < n; i++) {
    cout << ans[i] << " ";
  }
  cout << endl;
}

#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
#define int long long
using namespace std;
inline int read()
{
	bool flag=1;
	int x=0;
	char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-')
			flag=0;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=(x<<1)+(x<<3)+c-'0';
		c=getchar();
	}
	return (flag?x:~(x-1));
}
int n,now=18;
signed main()
{
	n=read()-3;
	cout<<"6 10 15 ";
	while(n)
	{
		if(now%30==0||now%30==6||now%30==10||now%30==12||now%30==15||now%30==18||now%30==24||now%30==20)
		{
			cout<<now<<" ";
			n-=1;
		}
		now++;
	}
	return 0;
}


#include <iostream>
#include <string>
#include <vector>
#include <map>
using ll=long long;
using namespace std;

int main(){
    int N;
    ll X;
    cin>>N>>X;

    vector<ll> A(N);
    for(int i=0;i<N;i++){
        cin>>A[i];
    }
    vector<ll> C(N+1);
    C[0]=1000000000000000000;
    for(int i=1;i<=N;i++){
        C[i]=A[N-i];
    }

    vector<map<ll,ll>> dp(N+1);
    dp[0][X]=1;
    for(int i=1;i<N+1;i++){
        for(auto p:dp[i-1]){
            ll a=p.first%C[i];
            dp[i][a]+=p.second;
            ll b=C[i]-a;
            ll num=p.first/C[i]+1;
            if(C[i]*num<C[i-1] && a!=0){
                dp[i][b]+=p.second;
            }
        }
    }
    /*
    for(int i=0;i<N+1;i++){
        for(auto p:dp[i]){
            cout<<"{"<<p.first<<","<<p.second<<"} ";
        }
        cout<<endl;
    }
    //*/
    cout<<dp[N][0]<<endl;

    return 0;
}

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define endl "\n"
#define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "

// const ll mod = 1e9 + 7;
const ll mod = 998244353;
const ll sz = 1e3 + 5;
vector<ll> fac(sz, 1);
vector<bool> prime(sz, true);
vector<ll> fib(sz, 0);

ll powmod(ll a, ll b) { ll res=1; a%=mod; assert(b>=0); for(;b;b>>=1) { if(b&1) res=res*a%mod; a=a*a%mod; } return res; }
ll nCr(ll n, ll r) { return ( ( ( (fac[n] * powmod(fac[n-r], mod-2)) % mod ) * powmod(fac[r], mod-2) ) % mod ); }
ll gcd(ll a, ll b) { return b ? gcd(b,a%b) : a; }
ll lcm(ll a, ll b) { return (a / gcd(a,b)) * b; }

// * Always PASS arrays, strings etc to functions BY REFERENCE

void solve() {
    int n, m;
    cin >> n >> m;

    priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>> > pq;
    vector<string> arr(n);
    vector<vector<int>> dist(n, vector<int>(m, 1e9));
    vector<vector<pair<int,int> > > pos(26);
    for (int j = 0; j < n; j++) {
        cin >> arr[j];
        for (int i = 0; i < m; i++) {
            if (arr[j][i] == 'S') {
                pq.push({0, j, i});
                dist[j][i] = 0;
            }
            else if (arr[j][i] >= 'a' && arr[j][i] <= 'z')
                pos[arr[j][i]-'a'].push_back({j,i});
        }
    }

    while (!pq.empty()) {
        auto fr = pq.top();
        pq.pop();
        int len = fr[0], i = fr[1], j = fr[2];

        if (arr[i][j] == 'G') {
            cout << dist[i][j] << endl;
            return;
        }

        vector<int> dx = {-1, 1, 0, 0};
        vector<int> dy = {0, 0, 1, -1};

        for (int k = 0; k < 4; k++) {
            int ii = i + dx[k], jj = j + dy[k];
            if (ii < 0 || ii >= n || jj < 0 || jj >= m || arr[ii][jj] == '#')
                continue;
            if (dist[ii][jj] <= len + 1)
                continue;

            dist[ii][jj] = len + 1;
            pq.push({dist[ii][jj], ii, jj});
        }

        if (!(arr[i][j] >= 'a' && arr[i][j] <= 'z'))
            continue;

        for (auto p: pos[arr[i][j]-'a']) {
            int ii = p.fi, jj = p.se;
            if (dist[ii][jj] <= len + 1)
                continue;
            dist[ii][jj] = len + 1;
            pq.push({dist[ii][jj], ii, jj});
        }
      	pos[arr[i][j]-'a'].clear();
    }

    cout << -1 << endl;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

//     fib[1] = 1;
//     for (int i = 2; i < sz; i++)
//         fib[i] = (fib[i-1] + fib[i-2]) % mod;

//     for (ll i = 2; i < sz; i++)
//         fac[i] = (fac[i-1] * i) % mod;

//     prime[0] = prime[1] = false;
//     for (ll i = 2; i*i < sz; i++) {
//         if (!prime[i])
//             continue;

//         for (ll j = i*i; j < sz; j += i)
//             prime[j] = false;
//     }

    int t = 1;
    // cin >> t;

    while(t--) {
        solve();
    }

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using ull=unsigned long long;
#define rep(i,n) for(int i=0; i<(n); i++)

ll GCD(ll a,ll b){ return b?GCD(b,a%b):a; }

ll A,B;
int E[73][73];

int pN;
int I[38];
ll G[38];
int dp1[1<<19], dp2[1<<19];
ll solve(){
  rep(i,pN) G[i]=0;
  rep(i,pN) rep(j,pN) if(!E[I[i]][I[j]]) G[i]|=(1ll<<j);
  int N1=(pN+1)/2, N2=pN/2;
  ll N1mask = (1ll<<N1)-1;
  rep(i,1<<N1) dp1[i]=0;
  rep(i,1<<N2) dp2[i]=0;
  dp1[0]=dp2[0]=1;
  rep(d,N1) rep(p,1<<N1) if(!(p&(1<<d))) if(!(p&G[d])) dp1[p|(1<<d)]+=dp1[p];
  rep(d,N2) rep(p,1<<N2) if(!(p&(1<<d))) if(!(p&(G[d+N1]>>N1))) dp2[p|(1<<d)]+=dp2[p];
  rep(d,N2) rep(p,1<<N2) if(!(p&(1<<d))) dp2[p|(1<<d)]+=dp2[p];
  ll ans=0;
  rep(p,1<<N1){
    ll mask = (1ll<<N2)-1;
    rep(d,N1) if(p&(1ll<<d)) mask &= ~(G[d]>>N1);
    ans += dp1[p]*dp2[mask];
  }
  return ans;
}

ll solve2(int x){
  int N1=(pN+1)/2, N2=pN/2;
  ll N1mask = (1ll<<N1)-1;
  ll allMask = 0; rep(d,pN) if(!E[I[d]][x]) allMask |= (1ll<<d);
  ll ans=0;
  rep(p,1<<N1) if(!(p&allMask)){
    ll mask = (1ll<<N2)-1;
    mask &= ~(allMask>>N1);
    rep(d,N1) if(p&(1ll<<d)) mask &= ~(G[d]>>N1);
    ans += dp1[p]*dp2[mask];
  }
  return ans;
}

int main(){
  cin>>A>>B;
  int N = B-A+1;
  rep(i,N) rep(j,N) E[i][j]=((GCD(A+i,A+j)==1)?1:0);

  pN=0;
  rep(i,N) if((A+i)%2==1) I[pN++]=i;
  ll ans = solve();
  rep(i,N) if((A+i)%2==0) ans += solve2(i);
  cout<<ans<<endl;
  return 0;
}

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include <chrono>
#include <random>
#include <unordered_set>

using namespace std;

typedef long long ll;

void solve() {
    ll a, b;
    cin >> a >> b;
    int n = 73;
    vector<int> s(n);
    vector<int> prime;
    for (int i = 2; i < n; i++) {
        if (s[i] == 0) {
            s[i] = i;
            prime.emplace_back(i);
            for (int j = i; j < n; j += i) {
                s[j] = i;
            }
        }
    }

    int m = b - a + 1;
    n = prime.size();
    vector<int> mask(m);

    for (int i = 0; i < m; i++) {
        ll x = a + i;
        for (int j = 0; j < n; j++) {
            if (x % prime[j] == 0) {
                mask[i] ^= (1 << j);
            }
        }
    }

    vector<ll> dp(1 << n);
    dp[0] = 1;

    for (int i = 0; i < m; i++) {
        vector<ll> dp2(1 << n);
        for (int j = 0; j < (1 << n); j++) {
            if (dp[j] == 0)
                continue;
            dp2[j] += dp[j];
            if ((mask[i] & j) == 0)
                dp2[mask[i] ^ j] += dp[j];
        }
        dp.swap(dp2);
    }

    ll ans = 0;
    for (int i = 0; i < (1 << n); i++) {
        ans += dp[i];
    }

    cout << ans;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
#if defined(_DEBUG)
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int q = 1;
    //cin >> q;
    for (; q > 0; q--) {
        solve();
        //cout << endl;
    }
}


#pragma GCC optimize ("Ofast")
#include<bits/stdc++.h>
using namespace std;
inline int my_getchar(){
  static char buf[1048576];
  static int s = 1048576;
  static int e = 1048576;
  if(s == e && e == 1048576){
    e = fread(buf, 1, 1048576, stdin);
    s = 0;
  }
  if(s == e){
    return EOF;
  }
  return buf[s++];
}
inline void rd(long long &x){
  int k;
  int m=0;
  x=0;
  for(;;){
    k = my_getchar();
    if(k=='-'){
      m=1;
      break;
    }
    if('0'<=k&&k<='9'){
      x=k-'0';
      break;
    }
  }
  for(;;){
    k = my_getchar();
    if(k<'0'||k>'9'){
      break;
    }
    x=x*10+k-'0';
  }
  if(m){
    x=-x;
  }
}
struct MY_WRITER{
  char buf[1048576];
  int s;
  int e;
  MY_WRITER(){
    s = 0;
    e = 1048576;
  }
  ~MY_WRITER(){
    if(s){
      fwrite(buf, 1, s, stdout);
    }
  }
}
;
MY_WRITER MY_WRITER_VAR;
void my_putchar(int a){
  if(MY_WRITER_VAR.s == MY_WRITER_VAR.e){
    fwrite(MY_WRITER_VAR.buf, 1, MY_WRITER_VAR.s, stdout);
    MY_WRITER_VAR.s = 0;
  }
  MY_WRITER_VAR.buf[MY_WRITER_VAR.s++] = a;
}
inline void wt_L(char a){
  my_putchar(a);
}
inline void wt_L(int x){
  int s=0;
  int m=0;
  char f[10];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar('-');
  }
  while(s--){
    my_putchar(f[s]+'0');
  }
}
inline void wt_L(long long x){
  int s=0;
  int m=0;
  char f[20];
  if(x<0){
    m=1;
    x=-x;
  }
  while(x){
    f[s++]=x%10;
    x/=10;
  }
  if(!s){
    f[s++]=0;
  }
  if(m){
    my_putchar('-');
  }
  while(s--){
    my_putchar(f[s]+'0');
  }
}
long long A;
long long B;
long long C;
long long D;
int main(){
  int i;
  rd(A);
  rd(B);
  rd(C);
  rd(D);
  if(D*C - B == 0){
    wt_L(-1);
    wt_L('\n');
    exit(0);
  }
  long long k = A/(D*C - B);
  long long ans = -1;
  for(i=(-1);i<(2);i++){
    long long u = k+i;
    if(A + B * u <= D * C * u ){
      ans = u;
      break;
    }
  }
  if(ans < 0){
    ans = -1;
  }
  wt_L(ans);
  wt_L('\n');
  return 0;
}
// cLay version 20210405-1

// --- original code ---
// //no-unlocked
// ll A, B, C, D;
// {
//     rd(A, B, C, D);
//     if(D*C - B == 0){
//         wt(-1); exit(0);
//     }
//     ll k = A/(D*C - B);
//     ll ans = -1;
//     rep(i, -1, 2){
//         ll u = k+i;
//         if(A + B * u <= D * C * u ){
//             ans = u; break;
//         }
//     }
//     if(ans < 0) ans = -1;
//     wt(ans);
// }

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define pb push_back
template <class T, class U>
inline bool chmax(T& a, U b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T, class U>
inline bool chmin(T& a, U b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
constexpr int INF = 1000000000;
constexpr ll llINF = 1000000000000000000;
constexpr int mod = 1000000007;
constexpr double eps = 1e-10;
const double pi = acos(-1);
int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};
int Random(int mi, int ma) {
    random_device rnd;
    mt19937 mt(rnd());  // 32bit
    //[mi,ma]
    uniform_int_distribution<int> engine(mi, ma);
    return engine(mt);
}
/*
vector<vector<ll>>C,sC;
void init_comb(int n,int m){
    C.resize(n+1,vector<ll>(m+1,0));
    sC.resize(n+1,vector<ll>(m+1,0));
    C[0][0]=1;
    for(int i=1;i<=n;i++){
        C[i][0]=1;
        for(int j=1;j<=m;j++){
            C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod;
        }
    }
    rep(i,n+1){
        rep(j,m){
            sC[i][j+1]=(sC[i][j]+C[i][j])%mod;
        }
    }
}*/
bool prime(int a) {
    if (a == 1) return false;
    for (int i = 2; i * i <= a; i++) {
        if (a % i == 0) return false;
    }
    return true;
}
vector<int> primes;
void init_prime(int n) {
    primes.push_back(2);
    for (int i = 3; i <= n; i += 2) {
        bool f = true;
        for (int j : primes) {
            if (j * j > i) break;
            if (i % j == 0) {
                f = false;
                break;
            }
        }
        if (f) primes.push_back(i);
    }
}
ll modpow(ll a, ll b) {
    ll res = 1;
    while (b) {
        if (b & 1) {
            res *= a;
            res %= mod;
        }
        a *= a;
        a %= mod;
        b >>= 1;
    }
    return res;
}
vector<ll> inv, fact, factinv;
void init_fact(int n) {
    inv.resize(n + 1);
    fact.resize(n + 1);
    factinv.resize(n + 1);
    inv[0] = 0;
    inv[1] = 1;
    fact[0] = 1;
    factinv[0] = 1;
    for (ll i = 1; i <= n; i++) {
        if (i >= 2) inv[i] = mod - ((mod / i) * inv[mod % i] % mod);
        fact[i] = (fact[i - 1] * i) % mod;
        factinv[i] = factinv[i - 1] * inv[i] % mod;
    }
}
ll _inv(ll a, ll m = mod) {
    // gcd(a,m) must be 1
    ll b = m, u = 1, v = 0;
    while (b) {
        ll t = a / b;
        a -= t * b;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    u %= m;
    if (u < 0) u += m;
    return u;
}
ll comb(int a, int b) {
    if (a < b) return 0;
    if (a < 0) return 0;
    return fact[a] * factinv[a - b] % mod * factinv[b] % mod;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll a, b, c, d;
    cin >> a >> b >> c >> d;
    // a+bx,cx
    // a+bx<=cdx
    // a<=(cd-b)x
    if (c * d - b <= 0) {
        cout << -1 << endl;
        return 0;
    }
    // a/(cd-b)<=x
    ll e = (c * d - b);
    if (a % e == 0)
        cout << a / e << endl;
    else
        cout << a / e + 1 << endl;
}


#include<bits/stdc++.h>
using namespace std;

int main(){
  	int n,m;
	int i,j;
  	int count=0;
  	cin >> n;
  	if(n%100!=0){
    	cout << n/100+1 << endl;
    }else{
    	cout << n/100 << endl;
    }
}

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

#define rep(i,cc,n) for(int i=cc;i<n;++i)
#define lrep(i,cc,n) for(long long i=cc;i<n;++i)
#define sqrep(i,cc,n) for(long long i=cc;i*i<=n;++i)
#define rrep(i,cc,n) for(long i=cc;i>n;--i)
#define pii pair<int, int>
#define pll pair<long long, long long>

using ll = long long;
//using mint = modint;

const vector<int> dx = {1, 1,  1, 0,  0, 0, -1, -1, -1};
const vector<int> dy = {1, 0, -1, 1, -1, 0,  1,  0, -1};
const double PI = 3.141592653589793;
const ll inf = 1001001001;
const ll e9  = 1000000000;
const ll mod = 1000000007;
const ll mod2 = 998244353;

const int MAX = 1000000;
const ll MOD = 998244353;
const ll big = 1ll<<60;

ll gcd(ll x, ll y) { return (x % y)? gcd(y, x % y): y; }

int main(){
    int n;
    cin >> n;
    cout << (n+99)/100 << endl;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
typedef double db;
#define fi first
#define se second
const int MAXN=2e5+10;
int a[MAXN], n, pa[MAXN], cnt;
set<int> s;
inline void pre(int siz) { iota(pa+1, pa+1+siz, 1); cnt=siz; }
int find(int u) { return u==pa[u]?u:(pa[u]=find(pa[u])); }
inline bool isunion(int u,int v) { return find(u)==find(v); }
inline void merge(int u,int v) { if( isunion(u, v) ) return ; pa[find(u)]=find(v); --cnt; }
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin>>n;
    pre(2e5);
    cnt=0;
    for(int i=1;i<=n;++i){
        cin>>a[i];
        if(s.find(a[i])!=s.end()) continue;
        s.insert(a[i]);
        ++cnt;
    }
    int now=cnt;
    for(int l=1, r=n;l<r;++l, --r) merge(a[l], a[r]);
    cout<<now-cnt;
    cout.flush();
    return 0;
}

#include<bits/stdc++.h>
#define MAXN 300005
#define INF 1000000000000000000LL
#define MOD 1000000007
#define F first
#define S second
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int n,a[MAXN];
map<ll,int> cnt;
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    ll cur=0,ans=0;
    cnt[cur]++;
    for(int i=1;i<=n;i++)
    {
        if(i&1) cur+=a[i]; else cur-=a[i];
        ans+=cnt[cur];
        cnt[cur]++;
    }
    printf("%lld\n",ans);
    return 0;
}


#include <sstream>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <complex>
#include <malloc.h>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <bitset>
#include <list>
#include <string.h>
#include <assert.h>
#include  <time.h>
using namespace std;

#define ll long long
#define lp(i, e, n) for(ll i = e; i < n; i++)
#define IO ios::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define TC(ES) while (ES--)
#define fen kmosidnonuw
#define jen nrijngijerngair
#define jbw nguinerugnru
#define mx 0
//inevitability is the unavailabilty of vulnerability.
typedef int int32_t;
typedef pair<int, int> ii;
vector<int> v;

const int maxn = 2e5 + 17;
//989
void ontheGround(){
	string s;
	for(int i = 1; i <= 3; i++){
		cin >> s[i];
	}
	cout << s[2] << s[3] << s[1];
}

/*
no thats not me.
*/
int main(){
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	IO;
	ll ES = 1;
	//cin >> ES;
	TC(ES){
		ontheGround();
	}

return mx;
}

#include <bits/stdc++.h>
using namespace std;
long long INF = 1LL<<60;
using ll = long long;
using vll = vector<ll>;
using mll = map<ll, ll>;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rrep(i,a,b) for(int i=a;i>=b;i--)
#define fore(i,a) for(auto &i:a)
#define all(x) (x).begin(),(x).end()
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }

void solve(std::string S){
    cout<<S.substr(1) << S[0]<<endl;
}

int main(){
    std::string S;
    std::cin >> S;
    solve(S);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int n;
string s;

int ans(){
    if(s.front()!=s.back()) return 1;
    for(int sz=s.size(),i=1; i<sz; ++i){
        if(s[i-1]!=s[0] && s[i]!=s[0]) return 2;
    }
    return -1;
}

int main(){
    cin>>n>>s;
    cout<<ans();
}

#define DEBUG 0

#include <bits/stdc++.h>
using namespace std;

#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}

// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
    public:
        template<typename T>
        _Debug& operator,(T val) {
            cout << val << endl;
            return *this;
        }
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif

// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back

// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;

// ---------- END OF TEMPLATE ----------

int main() {
    int N;
    string S;
    cin >> N >> S;

    if (S[0] != S.back()) cout << 1 << endl;
    else {
        int i;
        for (i = 0; i < S.size()-1; i++) {
            if ((S[i] != S[0]) && (S[i+1] != S.back())) {
                cout << 2 << endl;
                return 0;
            }
        }
        cout << -1 << endl;
    }

    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;
ll n,k,ans;

ll sum(ll x){
	if(x>n+1)return 2*n-x+1;
	return x-1;
}

int main(){
	cin>>n>>k;
	k=abs(k);
	for(int i=2;i+k<=2*n;i++)ans+=sum(i)*sum(i+k);
	cout<<ans<<endl;
	return 0;
}

#include <bits/stdc++.h>
using namespace std;

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;

#define int                     long long int
#define ld                      long double
#define pb                      push_back
#define MOD                     1000000007
#define inf                     3e18
#define vi                      vector<int>
#define vld                     vector<ld>
#define pii                     pair<int,int>
#define mii                     map<int,int>
#define fi                      first
#define se                      second
#define fastIO                  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define db(...)                 __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) { cerr << " "<< name << " : " << arg1 <<'\n'; }
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
    const char* comma = strchr(names + 1, ',');
    cerr.write(names, comma - names) << " : " << arg1 << " | "; __f(comma + 1, args...);
}

typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> pbds;
		//order_of_key (k) : Number of items strictly smaller than k .
		//find_by_order(k) : K-th element in a set (counting from zero) (returns an iterator)

void inp_out()
{
	#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
	freopen("debug.txt", "w", stderr);
	#endif
}

int32_t main()
{
	fastIO
	inp_out();
	int n, x;
	cin >> n >> x;
	vector < pii > a(n);
	for(int i = 0; i < n; ++ i)
		cin >> a[i].fi >> a[i].se;
	x *= 100;
	int cur = 0;
	for(int i = 0; i < n; ++ i)
	{
		cur += (a[i].fi * a[i].se);
		if(cur > x)
		{
			cout << i + 1 << '\n';
			return 0;
		}
	}
	cout <<  - 1;
	return 0;
}


#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
void e(vector<ll> a, vector<ll>& p){
  ll c=a.size(),d=p.size();
  for(ll bits=0; bits<d; ++bits){
    ll sum=0;
    for(int i=0; i<c; ++i){
      if(!(bits&(1<<i))) continue;
      sum+=a[i];
    }
    p[bits]=sum;
  }
  sort(p.begin(),p.end());
}
int main(void){
  ll n,t; cin>>n>>t;
  vector<ll> a(n>>1),b((n+1)>>1),p(1<<(n>>1)),q(1<<((n+1)>>1));
  for(auto& i:a) cin>>i;
  for(auto& i:b) cin>>i;
  e(a,p); e(b,q);
  ll res=0;
  for(auto& i:p){
    if(i>t) break;
    res=max(res,i+*(upper_bound(q.begin(),q.end(),t-i)-1));
  }
  cout<<res;
  return 0;
}

#include <bits/stdc++.h>
using namespace std;

using   ll  =       long long int;
using   pll =       pair<ll, ll>;
using   vll =       vector<ll>;
#define endl        "\n"
#define space       " "
#define TLE_na_ho   ios_base::sync_with_stdio(false);cin.tie(NULL)
#define MAX_SIZE    1024
#define MOD         1000000007
#define pb          push_back
#define fi          first
#define se          second
#define all(v)      v.begin(),v.end()
#define f(n)        for(ll i=0;i<n;i++)
#define rep(i,a,n)  for(ll i=a;i<n;i++)
#define repe(i,a,n) for(ll i=a;i<=n;i++)
#define repr(i,a,n) for(ll i=a;i>=n;i--)
#define ain(arr,n)  for(ll i1=0;i1<n;i1++ ) cin>>arr[i1]
#define aout(arr,n) for(ll i1=0;i1<n;i1++ ) cout<<arr[i1]<<space

ll testcases;

int main()
{
    TLE_na_ho;
    //READ THE QUESTION PROPERLY!!
    testcases = 1;
    //cin>>testcases;
    while(testcases--)
    {
        ll a, b;
        cin >> a >> b;
        ll sum = a + b;
        ll x = sum / 2;
        ll y = a - x;
        cout << x << space << y;
    }

}


#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = long long;
using P = pair<ll, ll>;
const ll INF = ll(10000);

void solve(long long N) {
    set<ll> ans;

    N -= 3;

    for (ll i = 12; i <= INF; i += 6) {
        ans.insert(i);
    }
    for (ll i = 20; i <= INF; i += 10) {
        ans.insert(i);
    }
    for (ll i = 30; i <= INF; i += 15) {
        ans.insert(i);
    }

    cout << 6 << " " << 10 << " " << 15 << " ";
    for (ll a : ans) {
        if (N == 0) {
            break;
        }
        cout << a << " ";
        --N;
    }
    cout << endl;
}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main() {
    long long N;
    scanf("%lld", &N);
    solve(N);
    return 0;
}

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define deb(x) cout<< #x << " " << x << "\n";
#define MAX 9223372036854775807
#define MIN -9223372036854775807
#define setbits(n) __builtin_popcountll(n)
#define mkunique(a) a.resize(unique(a.begin(),a.end())-a.begin()); //need to sort
#define print(s) for(ll u: s) cout<<u<<" "; cout<<"\n";
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll mod=1e9+7;

int main(){

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    ll TT=clock();

    vector<ll> a;
    ll lim=10000;
    set<ll> s;
    ll val=3*5;
    ll j=1;
    while(1){
        if(s.find(val*j)!=s.end()) {j++;continue;}
        if(val*j>lim) break;
        a.pb(val*j);
        s.insert(val*j);
        j++;
    }
    //cout<<a.size()<<"\n";
    val=2*3; j=1;
    while(1){
        if(s.find(val*j)!=s.end()) {j++;continue;}
        if(val*j>lim) break;
        a.pb(val*j);
        s.insert(val*j);
        j++;
    }
    //cout<<a.size()<<"\n";
    val=2*5; j=1;
    while(1){
        if(s.find(val*j)!=s.end()) {j++;continue;}
        if(val*j>lim) break;
        a.pb(val*j);
        s.insert(val*j);
        j++;
    }
    ll n;
    cin>>n;
    vector<ll> b;
    if(n<=666){
        for(ll i=0;i<n-2;i++)
            b.pb(a[i]);
        b.pb(2*3); b.pb(2*5);
    }
    else if(n<=1999){
        for(ll i=0;i<n-1;i++)
            b.pb(a[i]);
        b.pb(2*5);
    }
    else{
        for(ll i=0;i<n;i++)
            b.pb(a[i]);
    }
    ll gc=b[0];
    for(ll i=0;i<n;i++){
        gc=__gcd(gc,b[i]);
        cout<<b[i]<<" ";
    }
    assert(gc==1);

    cerr<<"\n\nTIME: "<<(long double)(clock()-TT)/CLOCKS_PER_SEC<<" sec\n";
    TT = clock();
    return 0;
}


#include <bits/stdc++.h>
#include <iostream>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define speed                     \
    ios_base::sync_with_stdio(0); \
    cin.tie(0);                   \
    cout.tie(0);
#define ll long long
#define ld long double
#define pb push_back
#define mem1(a) memset(a, -1, sizeof(a))
#define mem0(a) memset(a, 0, sizeof(a))
#define setp(x) fixed << setprecision(x)
#define endl "\n"
#define mod 1000000007
#define mod1 998244353
#define ff first
#define ss second
#define MAX 500005
#define N 500
#define INF 1000000009
#define all(v) v.begin(), v.end()
#define sbit(a) __builtin_popcount(a)
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
typedef pair<ll, ll> pll;
typedef pair<pll, ll> ppl;
typedef map<ll, ll> mpll;
typedef map<vector<ll>, ll> mpvl;
ll power(ll x, ll y)
{
    ll res = 1;

    x = x;

    if (x == 0)
        return 0;
    while (y > 0)
    {

        if (y & 1)
            res = (res * x);

        y = y >> 1;
        x = (x * x);
    }

    return res;
}
void solve()
{
    ll n, m;
    cin >> n >> m;
    vector<ll> a;
    for (ll i = 0; i < m; i++)
    {
        ll x;
        cin >> x;
        a.pb(x);
    }
    a.pb(0);
    a.pb(n);
    sort(all(a));
    if (n == m)
    {
        cout << 0 << endl;
        return;
    }
    if (m == 0)
    {
        cout << 1 << endl;
        return;
    }
    vector<ll> v;
    ll len = a.size();
    for (ll i = 1; i < len - 1; i++)
    {
        v.pb(a[i] - a[i - 1] - 1);
    }
    v.pb(a[len - 1] - a[len - 2]);
    ll mn = 1e18;
    for (auto it : v)
    {
        if (it != 0)
        {
            mn = min(mn, it);
        }
    }
    ll ans = 0;
    for (auto it : v)
    {
        ans += (it / mn);
        if (it % mn)
            ans++;
    }
    cout << ans << endl;
}
int main()
{

    speed;
    ll kk;
    kk = 1;
    //cin >> kk;
    while (kk--)
    {
        solve();
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#pragma comment(linker, "/stack:200000000")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//turn on extra precision
//#pragma GCC target("fpmath=387")

using namespace std;
using namespace __gnu_pbds;

typedef long long ll;
typedef string str;
typedef pair <int,int> pii;
typedef pair <ll,ll> pll;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <pii> vpii;
typedef vector <pll> vpll;

#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update>
#define mp make_pair
#define pb push_back
#define pob pop_back
#define pf push_front
#define pof pop_front
#define fi first
#define se second
#define fs first.second
#define ss second.second
#define ff first.first
#define sf second.first
#define newl '\n'
#define fbo find_by_order
#define ook order_of_key
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(),x.rend()
#define watch(x) cout << (#x) << " is : " << (x) << newl

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
vi dirx = {0,0,1,-1};
vi diry = {1,-1,0,0};
char to_upper (char x){
    if( 97 <= int(x) && int(x) <= 122) return char(x-32);
    if( 65 <= int(x) && int(x) <= 90) return x;
    return -1;
}
char to_lower (char x){
    if( 97 <= int(x) && int(x) <= 122) return x;
    if( 65 <= int(x) && int(x) <= 90) return char(x+32);
    return -1;
}
int numerize (char x){
    if(48 <= int(x) && int(x) <= 57) return int(x-'0');
    if(97 <= int(x) && int(x) <= 122) return int(x-96);
    if(65 <= int(x) && int(x) <= 90) return int(x-64);
    return -1;
}
bool isect (int l1, int r1, int l2, int r2){ return max(l1,l2) <= min(r1,r2); }
ll quickpow (ll num1, ll num2, ll MOD){
    if(num2==0)return 1%MOD;
    else if(num2==1)return num1%MOD;
    else{
        ll temp = quickpow (num1,num2>>1LL,MOD); ll res = ((temp%MOD) * (temp%MOD))%MOD;
        if(num2&1) res = ((res%MOD)*(num1%MOD))%MOD; return res;
    }
}
ll invmod (ll num, ll MOD){return quickpow (num,MOD-2,MOD);}
ll gcd (ll num1, ll num2){
    if(num1 < num2) swap(num1,num2); ll num3 = num1 % num2 ;
    while(num3 > 0){ num1 = num2; num2 = num3; num3 = num1 % num2;}
    return num2;
}
ll lcm (ll num1 , ll num2){return (ll) (num1/__gcd(num1,num2))*num2;}
// end of Template

const int MAXN = 2e5 + 5;
const ll INF = 1e17;
int n;
ll lo, hi, a[MAXN], t[MAXN];

ll f(ll x){
    for(int i = 1; i <= n; ++i) {
        if(t[i] == 1) x += a[i];
        if(t[i] == 2) x = max(x,a[i]);
        if(t[i] == 3) x = min(x,a[i]);
//        watch(x);
    }
    return x;
}

ll binserlo(){
    ll l = -INF, r = INF, res = -1;
    while(l <= r){
        ll mid = (l + r) >> 1LL;
        if(f(mid) <= lo) res = mid, l = mid + 1;
        else r = mid - 1;
    }
    return res;
}

ll binserhi(){
    ll l = -INF, r = INF, res = -1;
    while(l <= r){
        ll mid = (l + r) >> 1LL;
        if(f(mid) >= hi) res = mid, r = mid - 1;
        else l = mid + 1;
    }
    return res;
}
int main(){

//    freopen("input.txt", "r", stdin);
//    freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n; for(int i = 1; i <= n; ++i) cin >> a[i] >> t[i];
    lo = f(-INF), hi = f(INF);
    ll L = binserlo(), R = binserhi();

    int q; cin >> q; while(q--){
        ll x, res; cin >> x;
        if(lo == hi) {cout << lo << newl; continue; }

        if(x < L) res = lo;
        else if(x > R) res = hi;
        else res = lo + (x - L);
        cout << res << newl;
    }

    return 0;
}

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
const long long INF = 1e17;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);

    long long n;
    cin >> n;
    long long f0 = 0, l = -INF, r = INF;
    long long memo = 0;
    for(long long i = 0; i < n; i++){
        long long a, t;
        cin >> a >> t;
        if(t == 1){
            memo += a;
            f0 += a;
            l += a;
            r += a;
        } else if(t == 2){
            f0 = max(f0, (long long)a);
            l = max(l, (long long)a);
            r = max(r, (long long)a);
        } else {
            f0 = min(f0, (long long)a);
            l = min(l, (long long)a);
            r = min(r, (long long)a);
        }

    }

    long long q;
    cin >> q;
    vector<long long> ans(q);
    for(long long i = 0; i < q; i++){
        long long x;
        cin >> x;
        if(l-memo > x) ans[i] = l;
        else if(r-memo < x) ans[i] = r;
        else ans[i] = x+memo;
    }

    for(long long i = 0; i < q; i++) cout << ans[i] << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main() {
 ios::sync_with_stdio(0);
 cin.tie(0);
 cout.tie(0);
 ll a,b;
 cin>>a>>b;
 ll ans=1;
 if(b>=2*a){
 for(ll i=b;i>=a;i--){
  if(i*2<=b){
    ans=i;
    break;
  }
 }
 cout<<max(ans,(ll)1);
}
else{
  ll ans=1;
  for(ll i=1;i<=b;i++){
    int ans1=b/i-a/i;
    if(a%i==0)ans1++;
    if(ans1>1)ans=max(ans,i);
  }
  cout<<ans;
}
}

#include <bits/stdc++.h>
using namespace std;

#define REP(i,n) for(ll i=0;i<(ll)n;i++)
#define dump(x)  cerr << "Line " << __LINE__ << ": " <<  #x << " = " << (x) << "\n";
#define spa << " " <<
#define fi first
#define se second
#define ALL(a)  (a).begin(),(a).end()
#define ALLR(a)  (a).rbegin(),(a).rend()

using ld = long double;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using pdd = pair<ld, ld>;

template<typename T> using V = vector<T>;
template<typename T> using P = pair<T, T>;
template<typename T> vector<T> make_vec(size_t n, T a) { return vector<T>(n, a); }
template<typename... Ts> auto make_vec(size_t n, Ts... ts) { return vector<decltype(make_vec(ts...))>(n, make_vec(ts...)); }
template<class S, class T> ostream& operator << (ostream& os, const pair<S, T> v){os << "(" << v.first << ", " << v.second << ")"; return os;}
template<typename T> ostream& operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; }
template<class T> ostream& operator<<(ostream& os, const vector<vector<T>> &v){ for(auto &e : v){os << e << "\n";} return os;}
struct fast_ios { fast_ios(){ cin.tie(nullptr); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;

template <class T> void UNIQUE(vector<T> &x) {sort(ALL(x));x.erase(unique(ALL(x)), x.end());}
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
void fail() { cout << -1 << '\n'; exit(0); }
inline int popcount(const int x) { return __builtin_popcount(x); }
inline int popcount(const ll x) { return __builtin_popcountll(x); }
template<typename T> void debug(vector<vector<T>>&v,ll h,ll w){for(ll i=0;i<h;i++)
{cerr<<v[i][0];for(ll j=1;j<w;j++)cerr spa v[i][j];cerr<<"\n";}};
template<typename T> void debug(vector<T>&v,ll n){if(n!=0)cerr<<v[0];
for(ll i=1;i<n;i++)cerr spa v[i];
cerr<<"\n";};

const ll INF = (1ll<<62);
// const ld EPS   = 1e-10;
// const ld PI    = acos(-1.0);
const ll mod = (int)1e9 + 7;
//const ll mod = 998244353;

bool is_prime(int64_t x) {
  for(int64_t i = 2; i * i <= x; i++) {
    if(x % i == 0) return false;
  }
  return true;
}

int main(){

    ll A, B;
    cin >> A >> B;

    V<ll> primes;
    for(ll i=2;i<=72;i++){
        if(is_prime(i)){
            primes.push_back(i);
        }
    }

    ll sz = primes.size();

    V<ll> dp(1ll<<sz, 0);
    dp[0] = 1;

    for(ll v=A;v<=B;v++){
        ll bit = 0;
        REP(i, sz){
            if(v % primes[i] == 0){
                bit += (1ll<<i);
            }
        }
        V<ll> td(1ll<<sz, 0);
        REP(mask, 1ll<<sz){
            td[mask] += dp[mask];
            if((mask & bit) == 0){
                td[mask | bit] += dp[mask];
            }
        }
        swap(dp, td);
    }

    ll res = 0;
    for(auto x: dp) res += x;
    cout << res << endl;

    // dump(primes)
    // dump(sz)

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define INF 200005
#define MOD 1000000007
#define ping cout << "ping\n"

int main()
{
	//freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    //ios_base::sync_with_stdio(false);
    //cin.tie(NULL);
    int n;
    cin >> n;
    string a;
    cin >> a;
    int counter=0;
    for(int i=0;i<n;i++)
    {
    	int aa=0;
    	int tt=0;
    	int cc=0;
    	int gg=0;
    	for(int k=i;k<n;k++)
    	{
    		if(a[k]=='A') aa++;
    		else if(a[k]=='T') tt++;
    		else if(a[k]=='C') cc++;
    		else if(a[k]=='G') gg++;
    		if(aa==tt && cc==gg)
    		{
    			counter++;
    		}
    	}
    }
    cout << counter << "\n";
}

#ifdef DBG_MACRO_NO_WARNING
#include <dbg.h>
#endif
#include <bits/stdc++.h>
#define all(c) c.begin(), c.end()
#define isz(c) (int)c.size()

using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;

template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void read_n(InputIterator it, int n)
{
    copy_n(istream_iterator<T>(cin), n, it);
}

template <typename InputIterator,
          typename T = typename iterator_traits<InputIterator>::value_type>
void write(InputIterator first, InputIterator last, const char *delim = "\n")
{
    copy(first, last, ostream_iterator<T>(cout, delim));
}

int solve(int n, string s)
{
    map<char, vi> pm = {
        {'A', vi(n, 0)}, {'T', vi(n, 0)}, {'C', vi(n, 0)}, {'G', vi(n, 0)}};

    string opts = "AGCT";

    for (int i = 0; i < n; ++i)
    {
        for (auto c : opts)
        {
            pm[c][i] += s[i] == c;
            if (i != 0)
                pm[c][i] += pm[c][i - 1];
        }
    }

    auto sum = [&pm](int l, int r, char c) {
        return pm[c][r] - (l > 0 ? pm[c][l - 1] : 0);
    };

    int ans = 0;
    for (int l = 0; l < n - 1; ++l)
    {
        for (int r = l + 1; r < n; ++r)
        {
            ans += (sum(l, r, 'A') == sum(l, r, 'T') and
                    sum(l, r, 'C') == sum(l, r, 'G'));
        }
    }
    return ans;
}

int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    int    n;
    string s;
    cin >> n >> s;

    cout << solve(n, s) << endl;
    return 0;
}