import sys input = sys.stdin.readline from collections import Counter n=int(input()) S=input().strip() LEN=n+5 BIT=[0]*(LEN+1) # 1-indexedなtree. 配列BITの長さはLEN+1にしていることに注意。 def update(v,w): # index vにwを加える while v<=LEN: BIT[v]+=w v+=(v&(-v)) # v&(-v)で、最も下の立っているビット. 自分を含む大きなノードへ. たとえばv=3→v=4 def getvalue(v): # [1,v]の区間の和を求める ANS=0 while v!=0: ANS+=BIT[v] v-=(v&(-v)) # 自分より小さい自分の和を構成するノードへ. たとえばv=14→v=12へ return ANS A=[ord(S[i])-65 for i in range(n)] SA=0 AGCT=[0,2,6,19] C=Counter(A) ANS=0 while True: #print("SA",SA) count=0 for c in C: if (c+SA)%26 in AGCT: count+=C[c] if count==0: print(ANS) break else: ANS+=1 # ind-getvalue(ind)==count-1 #print("count",count) OK=-1 NG=n-1 while NG>OK+1: mid=(OK+NG)//2 if mid-getvalue(mid)>count-1: NG=mid else: OK=mid #print("OK",OK) update(OK+1,1) k=A[OK] C[k]-=1 SA+=C[k] #print(ANS)