Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

def main():
    N = int(input())
    S = input()
    R = set()
    G = set()
    B = set()
    for i,s in enumerate(S):
        if s == "R":
            R.add(i)
        elif s == "B":
            B.add(i)
        else:
            G.add(i)
    ans = 0
    for r in R:
        for b in B:
            for g in G:
                k = max(r, b, g)
                i = min(r, b, g)
                j = r + b + g - k - i
                if j - i != k - j:
                    ans += 1
    print(ans)

if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    N = int(input())
    S = input()
    cnt = 0
    for i in range(N):
        for j in range(i + 1, N):
            k = 2 * j - i
            if k >= N:
                continue
            if S[j] != S[i] and S[i] != S[k] and S[k] != S[j]:
                cnt += 1
    print(S.count("R") * S.count("B") * S.count("G") - cnt)

if __name__ == "__main__":
    main()
### BUGGY CODE ###

a,b=map(int,input().split())
h=[int(i) for i in input().split()]
ans=[[] for i in range(a)]
ansl=[]
anss=0
for i in range(b):
    c,d=map(int,input().split())
    ans[c-1].append(d-1)
    ans[d-1].append(c-1)
    ans[c-1]=list(set(ans[c-1]))
    ans[d-1]=list(set(ans[d-1]))
for i in ans:
    ansl+=[[h[j] for j in i]]
j=0
for i in range(a):
    height=h[i]
    if len(ansl[i])!=0:
        while True:
            if j==len(ansl[i]):
                anss+=1
                j=0
                break
            if ansl[i][j]>=height:
                j=0
                break
            j+=1
    else:
        anss+=1

print(anss)

### FIXED CODE ###

a,b=map(int,input().split())
h=[int(i) for i in input().split()]
ansl=[]
dic={i:0 for i in range(a)}
aans=[]
for i in range(b):
    c,d=map(int,input().split())
    if True:
        ch=h[c-1]
        dh=h[d-1]
        if ch==dh:
            if dic[c-1]==0:
                ansl.append(c-1)
                dic[c-1]+=1
            if dic[d-1]==0:
                ansl.append(d-1)
                dic[d-1]+=1
        else:
            if ch<dh:
                if dic[c-1]==0:
                    ansl.append(c-1)
                    dic[c-1]+=1
            else:
                if dic[d-1]==0:
                    ansl.append(d-1)
                    dic[d-1]+=1
        aans.append([c,d])
print(a-len(ansl))
### BUGGY CODE ###

N,K,Q = map(int,input().split())
A = [int(input()) for _ in range(Q)]
for i in range(N):
    katisuu = A.count(i+1)
    point = Q - katisuu

    if point < K:
        print("Yes")
    else:
        print("No")


### FIXED CODE ###

N,K,Q = map(int,input().split())
l = [0]*N

for i in range(Q):
    l[int(input())-1] += 1

for i in range(N):
    point = Q - l[i]

    if point < K:
        print("Yes")
    else:
        print("No")
### BUGGY CODE ###

num = input()
input_data = input().split()

result = 1
for i in range(int(num)):
  result = result * int(input_data[i])

if pow(10, 18) < result:
  print(-1)
else:
  print(result)

### FIXED CODE ###

num = int(input())
input_data = list(map(int,input().split()))

for i in input_data:
	if i == 0:
		print(0)
		exit()

result = 1
for i in range(num):
	result = result * int(input_data[i])
	if pow(10, 18) < result:
		print(-1)
		exit()

print(result)
### BUGGY CODE ###

while True:
    try:
        n = int(raw_input())
        filter = [1 for i in range(n)]
        filter[0] = 0
        for i in range(2,n//2):
            j = 2
            k = i*j
            while k < n:
                filter[k-1] = 0
                j += 1
                k = i*j
        print filter
        sum = 0
        for i in filter:
            sum += i
        print sum
    except:
        break

### FIXED CODE ###

import math
n = []
while True:
    try:
        n.append(int(raw_input()))
    except:
        break
  
r = max(n)+1
sqrt = int(math.sqrt(r))
p = [1]*r
p[0] = 0
p[3::2] = [0 for x in range(3,r,2)]
for i in range(2,sqrt,2):
    if p[i]:
        p[2*i+1::i+1] = [0 for x in range(2*i+1,r,i+1)] 
  
for i in n:
     print sum(p[:i])
### BUGGY CODE ###

n,k=map(int,input().split())
ans=0
x=list(range(n+1))
for i in range(k,n+2):
  A=sum(x[n-i+1:n+1])
  B=sum(x[0:i])
  ans+=A-B+1
print(ans)


### FIXED CODE ###

a,b=(int(x)for x in input().split())
z=0
while b<=a+1:z=(z+(a-b+1)*b+1)%(10**9+7);b+=1
print(z)
### BUGGY CODE ###

def test():
    n = int(input())
    a = [int(input()) for i in range(n)]
    s = 0
    for i in range(n):
        s = a[i]
        a[i] = 0
        print(max(a))
        a[i] = s


if __name__ == "__main__":
    test()


### FIXED CODE ###

def test():
    n = int(input())
    s = 0; t = 0; index_ = 0
    for i in range(n):
        a = int(input())
        if s < a:
            index_ = i
            s = a
        elif t < a and a <= s:
            t = a
    for i in range(n):
        if index_ == i:
            print(t)
        else:
            print(s)


if __name__ == "__main__":
    test()
### BUGGY CODE ###

#!/usr/bin/env python3
# スペース区切りの整数の入力
n, k = map(int, input().split())
#配列の入力
data = list(map(int, input().split()))

temp = 1
old = 1
for i in range(n):
    if i < k:
        temp *= data[i]
    else:
        old = temp
        temp = temp // data[i-k] * data[i]
    #print("i:{}, k:{}".format(i, k))
    #print(score)

    if i > k-1:
        if temp > old:
            print("Yes")
        else:
            print("No")




### FIXED CODE ###

#!/usr/bin/env python3
# スペース区切りの整数の入力
n, k = map(int, input().split())
#配列の入力
data = list(map(int, input().split()))

for i in range(n-k):
    if data[i] < data[k+i]:
        print("Yes")
    else:
        print("No")
### BUGGY CODE ###

n,m = map(int,input().split())
a = [int(i) for i in input().split()]

for i in range(m):
    b,c = map(int,input().split())
    for j in range(b):
        a.append(c)

a.sort(reverse=True)
print(sum(a[:n]))


### FIXED CODE ###

n,m = map(int,input().split())
a = [int(i) for i in input().split()]
bc = [[int(i) for i in input().split()] for i in range(m)]
ans = 0
cnt = 0
d = {}

for i in range(n): 
    d[a[i]] = d.get(a[i],0)+1
for k in d: 
    bc.append([d[k],k])
bc.sort(key= lambda x:x[1],reverse=True)

for i in range(len(bc)):
    if cnt < n:
        ans += bc[i][1]*bc[i][0]
        cnt += bc[i][0]
    else:
        ans -= (cnt-n)*bc[i-1][1]
        break
else:
    ans -= (cnt-n)*bc[i][1]

print(ans)
### BUGGY CODE ###

n,w,h = map(int,input().split())
Yoko = [0] * w
Tate = [0] * h
for i in range(n):
    x,y,r = map(int,input().split())
    for j in range(x - r,x + r):
        if 0 <= j < w:
            Yoko[j] += 1
    for j in range(y- r,y + r):
        if 0 <= j < h:
            Tate[j] += 1
    if Yoko.count(0) == 0 or Tate.count(0) == 0:
        break
if Yoko.count(0) == 0 or Tate.count(0) == 0:
    print("Yes")
else:
    print("No")

### FIXED CODE ###

import sys
n,w,h = map(int,input().split())
Yoko = []
Tate = []
for i in range(n):
    x,y,r = map(int,input().split())
    Yoko.append([x - r,x + r])
    Tate.append([y - r,y + r])
Yoko = sorted(Yoko)
Tate = sorted(Tate)

if Yoko[0][0] > 0 and Tate[0][0] > 0:
    print("No")
    sys.exit()

Yoko_han = [Yoko[0][0],Yoko[0][1]]
Tate_han = [Tate[0][0],Tate[0][1]]
if Yoko[0][0] <= 0:
    if Yoko_han[1] >= w:
        print("Yes")
        sys.exit()
    else:
        for j in range(1,len(Tate)):
            if Yoko[j][0] <= Yoko_han[1] and Yoko_han[1] < Yoko[j][1]:
                Yoko_han[1] = Yoko[j][1]
                if Yoko_han[1] >= w:
                    print("Yes")
                    sys.exit()
                
            elif Yoko_han[1] < Yoko[j][0]:
                break
if Tate[0][0] > 0:
    print("No")
    sys.exit()
else:
    if Tate_han[1] >= h:
            print("Yes")
            sys.exit()
    else:
        for k in range(1,len(Yoko)):
            if Tate[k][0] <= Tate_han[1] and Tate_han[1] < Tate[k][1]:
                Tate_han[1] = Tate[k][1]
                if Tate_han[1] >= h:
                    print("Yes")
                    sys.exit()
                
            elif Yoko_han[1] < Yoko[k][0]:
                print("No")
                sys.exit()
print("No")
### BUGGY CODE ###

# coding: utf-8
# Your code here!
from itertools import chain
from collections import Counter

N, M = map(int,input().split())
li = [list(map(int,input().split())) for i in range(M)]
arr = [[j for j in range(li[k][0], li[k][1] + 1)] for k in range(M)]
set_arr = list(chain.from_iterable(arr))
arr_duplicate = [l for l in Counter(set_arr).values() if l == M]
print(len(arr_duplicate))

### FIXED CODE ###

# coding: utf-8
# Your code here!

N, M = map(int,input().split())
li = [list(map(int,input().split())) for i in range(M)]
L = [li[j][0] for j in range(M)]
R = [li[k][1] for k in range(M)]
ctr = 0

if min(R) - max(L) >= 0:
    print(min(R) - max(L) + 1)
else:
    print(0)
### BUGGY CODE ###

from collections import deque
from collections import defaultdict
N = int(input())

def trans(x):
    return int(x)-1

A = deque()
for _ in range(N):
    A.append(deque(map(trans,input().split())))

Q = deque()

def check(i):
    if len(A[i]) == 0:
        return
    j = A[i][0]
    if len(A[j]) == 0:
        return
    if A[j][0] == i:
        if i > j:
            i, j = j, i
        Q.append([i, j])

def get_unique(q):
    r = deque()
    seen = defaultdict(int)
    for a in q:
        if seen[a[0]] == 0:
            seen[a[0]] = 1
            r.append(a)
    return r


for i in range(N):
    check(i)

ans = 0
while len(Q) > 0:
    ans += 1
    Q2 = get_unique(Q)
    Q = deque()
    for q in Q2:
        A[q[0]].popleft()
        A[q[1]].popleft()
    for q in Q2:
        check(q[0])
        check(q[1])

for i in range(N):
    if len(A[i]) > 0:
        print(-1)
        exit()

print(ans)

### FIXED CODE ###

from collections import deque
N = int(input())

def trans(x):
    return int(x)-1

A = deque()
for _ in range(N):
    A.append(deque(map(trans,input().split())))

Q = set()

def check(i):
    if len(A[i]) == 0:
        return
    j = A[i][0]
    if len(A[j]) == 0:
        return
    if A[j][0] == i:
        if i > j:
            i, j = j, i
        Q.add((i, j))

for i in range(N):
    check(i)

ans = 0
while len(Q) > 0:
    ans += 1
    Q2 = Q.copy()
    Q = set()
    for q in Q2:
        A[q[0]].popleft()
        A[q[1]].popleft()
    for q in Q2:
        check(q[0])
        check(q[1])

for i in range(N):
    if len(A[i]) > 0:
        print(-1)
        exit()

print(ans)
### BUGGY CODE ###

n = int(input())
items = [tuple(map(int, input().split())) for _ in range(n)]

q = int(input())
v_to_qi = [[] for _ in range(n)]
v_to_l = [[] for _ in range(n)]
for i in range(q):
    v, l = map(int, input().split())
    v -= 1
    v_to_qi[v].append(i)
    v_to_l[v].append(l)

MAX = 10**5
# dp[i+1][j] = (i番目まで見て重さの合計がj以下のの時の価値の最大値)
dp = [[0] * (MAX + 1) for _ in range(20)]
ans = [0] * q


def dfs(v, depth):
    for j in range(MAX + 1):
        if j < items[v][1]:
            dp[depth + 1][j] = dp[depth][j]
        else:
            dp[depth + 1][j] = max(dp[depth][j],
                                   dp[depth][j - items[v][1]] + items[v][0])

    if len(v_to_qi[v]) > 0:
        for qi, l in zip(v_to_qi[v], v_to_l[v]):
            ans[qi] = dp[depth + 1][l]

    if v * 2 + 1 < n:
        dfs(v * 2 + 1, depth + 1)
    if v * 2 + 2 < n:
        dfs(v * 2 + 2, depth + 1)


dfs(0, 0)
print(*ans, sep='\n')


### FIXED CODE ###

def main():
    import sys
    input = sys.stdin.readline

    n = int(input())

    L_MAX = 10**5
    D_TH = 10
    V_MAX = (1 << D_TH) - 1
    dp = [[0] * (L_MAX + 1)]
    # items = []
    items_v = []
    items_w = []

    for i in range(n):
        val, w = map(int, input().split())
        if i < V_MAX:
            i1 = i + 1
            i0 = (i + 1) >> 1
            dp_ = dp[i0][:]

            # for j in range(w):
            #     dp[i1][j] = dp[i0][j]
            for j in range(w, L_MAX + 1)[::-1]:
                if dp_[j] < dp_[j - w] + val:
                    dp_[j] = dp_[j - w] + val
            dp.append(dp_)
        else:
            # items.append((val, w))
            items_v.append(val)
            items_w.append(w)

    # items = tuple(items)

    q = int(input())
    for _ in range(q):
        v, l = map(int, input().split())
        v -= 1

        if v < V_MAX:
            ans = dp[v + 1][l]
        else:
            vv = []
            ww = []
            while v >= V_MAX:
                # vv.append(items[v - V_MAX][0])
                # ww.append(items[v - V_MAX][1])
                vv.append(items_v[v - V_MAX])
                ww.append(items_w[v - V_MAX])
                v = (v - 1) >> 1

            ans = dp[v + 1][l]
            # m = len(vv)
            m2 = 1 << len(vv)
            v_sub = [0] * m2
            w_sub = [0] * m2
            for bit in range(1, m2):
                bit_min = bit & (-bit)
                x = bit_min.bit_length() - 1

                s_w = w_sub[bit - bit_min] + ww[x]
                w_sub[bit] = s_w
                if s_w <= l:
                    s_val = v_sub[bit - bit_min] + vv[x]
                    v_sub[bit] = s_val
                    ans = max(ans, dp[v + 1][l - s_w] + s_val)

        print(ans)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

I=lambda:map(int,input().split());*_,k=I();s=lambda*a:sorted(i+j for i in I()for j in a)[:-k-1:-1];print(*s(*s(*I())))

### FIXED CODE ###

I=lambda:list(map(int,input().split()));x,y,z,k=I();s=lambda a,b:sorted(i+j for i in a for j in b)[:-k-1:-1];print(*s(s(I(),I()),I()))
### BUGGY CODE ###

n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
c.reverse()
#print(a)
#print(b)
#print(c)
pat=0
ikeep=-1
for i in range(n):
  x=b[i]
  if(x!=ikeep):
    ikeep=x
  else:
    pat+=paty*patz
    continue
  for j in range(n):
    y=a[j]
    paty=n
    if(x<=y):
      paty=j
      break
  for j in range(n):
    z=c[j]
    patz=n
    if(z<=x):
      patz=j
      break
  #print(paty,patz)
  pat+=paty*patz
print(pat)


### FIXED CODE ###

import bisect
n=int(input())
a=list(map(int,input().split()))
b=list(map(int,input().split()))
c=list(map(int,input().split()))
a.sort()
b.sort()
c.sort()
#print(a)
#print(b)
#print(c)
pat=0
ikeep=-1
for i in range(n):
  x=b[i]
  if(x!=ikeep):
    ikeep=x
  else:
    pat+=paty*patz
    continue
  paty=bisect.bisect_left(a,x)
  patz=n-bisect.bisect_right(c,x)
  #print(paty,patz)
  pat+=paty*patz
print(pat)
### BUGGY CODE ###

from sys import stdin, setrecursionlimit
from collections import deque

setrecursionlimit(10 ** 9)

H, W = map(int, stdin.readline().split())
field = [list(stdin.readline().rstrip()) for _ in range(H)]

inf = 10 ** 9

flip_cnt = [[inf] * W for _ in range(H)]
direction = [[0, 1], [1, 0]]

q = deque([])


def bfs(h, w):
    q.append([h, w])

    while q:

        h, w = q.popleft()

        for dh, dw in direction:
            nh = h + dh
            nw = w + dw

            if nh >= H or nw >= W:
                continue

            q.append([nh, nw])

            if field[nh][nw] == field[h][w]:
                flip_cnt[nh][nw] = min(flip_cnt[nh][nw], flip_cnt[h][w])
            else:
                flip_cnt[nh][nw] = min(flip_cnt[nh][nw], flip_cnt[h][w] + 1)


flip_cnt[0][0] = 0
bfs(0, 0)

if field[0][0] == field[H - 1][W - 1] == "#":
    flip_cnt[H - 1][W - 1] += 1

print((flip_cnt[H - 1][W - 1] + 1) // 2)

# for f in flip_cnt:
#     print(f)


### FIXED CODE ###

from sys import stdin, setrecursionlimit
from collections import deque

setrecursionlimit(10 ** 9)

H, W = map(int, stdin.readline().split())
field = [list(stdin.readline().rstrip()) for _ in range(H)]

inf = 10 ** 9

flip_cnt = [[inf] * W for _ in range(H)]
direction = [[0, 1], [1, 0]]

q = deque([])


def bfs(h, w):
    q.append([h, w])

    while q:

        h, w = q.popleft()

        for dh, dw in direction:
            nh = h + dh
            nw = w + dw

            if nh >= H or nw >= W:
                continue

            if field[nh][nw] == field[h][w]:
                if flip_cnt[nh][nw] <= flip_cnt[h][w]:
                    continue
                else:
                    flip_cnt[nh][nw] = flip_cnt[h][w]
                    q.append([nh, nw])
            else:
                if flip_cnt[nh][nw] <= flip_cnt[h][w] + 1:
                    continue
                else:
                    flip_cnt[nh][nw] = flip_cnt[h][w] + 1
                    q.append([nh, nw])


flip_cnt[0][0] = 0
bfs(0, 0)

if field[0][0] == field[H - 1][W - 1] == "#":
    flip_cnt[H - 1][W - 1] += 1

print((flip_cnt[H - 1][W - 1] + 1) // 2)

# for f in flip_cnt:
#     print(f)
### BUGGY CODE ###

s = input()
l = len(s)
mod = (10**9)+7

dp = [[0 for j in range(13)] for i in range(l)]

if s[0] == '?':
    dp[0][0:9] = [1 for x in range(10)]
else:
    dp[0][int(s[0])] = 1

for i in range(l-1):
    a = i+1
    x = s[a]
    if x == '?':
        for j in range(10):
            for k in range(13):
                dp[a][(k*10+j)%13] += dp[a-1][k]%mod
    else:
        for k in range(13):
            dp[a][(k*10+int(x))%13] += dp[a-1][k]%mod

print(dp[-1][5]%mod)

### FIXED CODE ###

s = input()
l = len(s)
mod = (10**9)+7

dp = [[0 for j in range(13)] for i in range(l)]

if s[0] == '?':
    dp[0][0:9] = [1 for x in range(10)]
else:
    dp[0][int(s[0])] = 1

for i in range(l-1):
    a = i+1
    x = s[a]
    if x == '?':
        for j in range(10):
            for k in range(13):
                dp[a][(k*10+j)%13] += dp[a-1][k]%mod
    else:
        for k in range(13):
            dp[a][(k*10+int(x))%13] += dp[a-1][k]%mod

print(dp[-1][5]%mod)
### BUGGY CODE ###

import sys
import copy

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	N = II()
	SS = IS()
	ans = 0
	for l in range(N-1):
		i = 1
		j = 0
		S = copy.copy(SS[l:])
		A = [0]*len(S)
		while (i < len(S)):
			while (i+j < len(S) and S[j] == S[i+j] and  j< i):
				j+=1
			A[i] = j
			if (j == 0):
				i+=1
				continue
			k = 1
			while (i+k < len(S) and k+A[k] < j):
				A[i+k] = A[k]
				k+=1
			i += k
			j -= k
		ans = max(max(A),ans)
	print(ans)

main()

### FIXED CODE ###

import sys
import copy

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]

def main():
	N = II()
	SS = IS()
	ans = 0
	for l in range(N-1):
		i = 1
		j = 0
		S = copy.copy(SS[l:])
		A = [0]*len(S)
		while (i < len(S)):
			while (i+j < len(S) and S[j] == S[i+j]):
				j+=1
			A[i] = j
			if (j == 0):
				i+=1
				continue
			k = 1
			while (i+k < len(S) and k+A[k] < j):
				A[i+k] = A[k]
				k+=1
			i += k
			j -= k
		for m in range(len(S)):
			ans = max(ans, min(m, A[m]))
	print(ans)

main()
### BUGGY CODE ###

x=int(input())
flag=True
l=[]
for i in range(2,int(x**0.5)+1):
  if x%i==0:
    l.append(i)
    #print(q)
while flag:
  for i in range(-100000,100000):
    for j in l:
      if (i+j)**5-i**5==x:
        flag=False
        a=i+j
        b=i
print(a,b)

### FIXED CODE ###

x=int(input())
for i in range(-200,200):
  for j in range(-200,200):
    if i**5-j**5==x:
      a=i
      b=j
print(a,b)
### BUGGY CODE ###

s = input()
t = input()
N, M = len(s), len(t)

if t in s:
    print(s.find(t) + len(t))

else:
    schr, indchr = dict(), dict()
    for i in range(N):
        if s[i] in schr:
            schr[s[i]].append(i+1) 
        else:
            schr[s[i]] = [i+1]
            indchr[s[i]] = 0

    schrset = set(schr)
    ans, pos, back = 0, 0, True
    for i in range(M):
        if t[i] in schrset:
            c = schr[t[i]]
            if c[-1] <= pos:
                ans += N - pos + c[0]
                pos = c[0]
                if not back:
                    for x in indchr.keys():
                        indchr[x] = 0
                    back = True
            else:
                while c[indchr[t[i]]] <= pos:
                    indchr[t[i]] += 1
                ans += c[indchr[t[i]]] - pos
                pos, back = c[indchr[t[i]]], False
        else:
            print(-1)
            exit()
    print(ans)

### FIXED CODE ###

s = input()
t = input()
N, M = len(s), len(t)

schr, indchr = dict(), dict()
for i in range(N):
    if s[i] in schr:
        schr[s[i]].append(i+1) 
    else:
        schr[s[i]] = [i+1]
        indchr[s[i]] = 0

schrset = set(schr)
ans, pos, pre, back = 0, 0, set(), True
for i in range(M):
    if t[i] in schrset:
        c = schr[t[i]]
        if c[-1] <= pos:
            ans += N - pos + c[0]
            pos = c[0]
            if not back:
                for x in indchr.keys():
                    indchr[x] = 0
                back, pre = True, set()
        else:
            j = indchr[t[i]]
            while c[j] <= pos:
                j += 1
            if j > 0:
                pre.add(t[i])
            ans += c[j] - pos
            pos, back, indchr[t[i]] = c[j], False, j
    else:
        print(-1)
        exit()
print(ans)