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

# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
# import sys
# sys.setrecursionlimit(10**4)
# import time

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


MOD = 998244353

N = d_in()
# start_time = time.time()
total = 3 ** N - (2**N - 2) * 3 - 3

# 1要素目: 最大の色(赤とする)の和
# 2要素目: i番目の整数までを考慮
# 3要素目: [both>0, 緑>0&青==0, 緑==0&青>0, both==0]
dp = np.zeros(300*(N+1)**2*4, dtype=int).reshape(-1, N+1, 4)
dp[0, 0, 3] = 1

sum_a = 0
for i in range(1, N+1):
    a = d_in()
    sum_a += a
    # 赤に入れる
    dp[a:, i, :] += dp[:-a, i-1, :]

    # 緑に入れる
    dp[:, i, 0] += dp[:, i-1, 0]
    dp[:, i, 0] += dp[:, i-1, 2]
    dp[:, i, 1] += dp[:, i-1, 1]
    dp[:, i, 1] += dp[:, i-1, 3]

    # 青に入れる
    dp[:, i, 0] += dp[:, i-1, 0]
    dp[:, i, 0] += dp[:, i-1, 1]
    dp[:, i, 2] += dp[:, i-1, 2]
    dp[:, i, 2] += dp[:, i-1, 3]
    if i % 10 == 0:
        dp %= MOD


print((total - dp[(sum_a+1)//2:, -1, 0].sum() * 3) % MOD)


### FIXED CODE ###

# -*- coding: utf-8 -*-
from sys import stdin
import numpy as np
# import sys
# sys.setrecursionlimit(10**4)
# import time

s_in = lambda: stdin.readline()[:-1]  # s = s_in()
d_in = lambda: int(stdin.readline())  # N = d_in()
ds_in = lambda: list(map(int, stdin.readline().split()))  # List = ds_in()


MOD = 998244353

N = d_in()
total = 3 ** N - (2**N - 2) * 3 - 3

a_list = []
for _ in range(N):
    a_list.append(d_in())
sum_a = sum(a_list)

# 1要素目: i番目の整数までを考慮
# 2要素目: [both>0, 緑>0&青==0, 緑==0&青>0, both==0]
# 3要素目: 最大の色(赤とする)の和
dp = np.zeros((N+1, 3, 1 + (sum_a+1)//2), dtype=int)
dp[0, 2, 0] = 1

for i in range(1, N+1):
    # 赤に入れる
    a = a_list[i-1]
    dp[i, :, a:] += dp[i-1, :, :-a]
    dp[i, :, -1] += dp[i-1, :, -a:].sum(axis=1)

    # 緑or青に入れる
    dp[i, 0, :] += (dp[i-1, 0, :] + dp[i-1, 1, :]) * 2
    dp[i, 1, :] += dp[i-1, 1, :]
    dp[i, 1, :] += dp[i-1, 2, :]

    if i % 10 == 0:
        dp[i, :, :] %= MOD

print((total - dp[-1, 0, -1] * 3) % MOD)
### BUGGY CODE ###

import numpy as np
n, k = map(int, input().split())
a = list(map(int, input().split()))
npa = np.array(a)


def diff(now, d):
    return abs(a[now] - a[now - d])

dp = [0] * n
dp[1] = diff(1, 1)
for depth in range(2, n):
    m = min(depth, k)
    dp[depth] = np.min(dp[depth - m:depth] + np.abs(npa[depth] - npa[depth - m : depth]))
print(dp[-1])


### FIXED CODE ###

n, k = map(int, input().split())
a = list(map(int, input().split()))


def diff(now, d):
    return abs(a[now] - a[now - d])

dp = [0] * n
dp[1] = diff(1, 1)
for depth in range(2, n):
    # dp[depth] = min(dp[depth - 1] + diff(depth, 1), dp[depth - 2] + diff(depth, 2))
    dp[depth] = min(dp[depth - x] + diff(depth, x) for x in range(1, min(depth, k) + 1))
print(dp[-1])
### BUGGY CODE ###

from collections import deque

N=int(input())
A=[list(map(int,input().split())) for _ in range(N)]
A=list(map(lambda x: deque(x), A))
#print(A)

ans=0

L=[]
for i in range(N):
  L.append(len(A[i]))
#print(L)

while 1:
  P=[0]*N
  sw=0
  for i in range(N):
    if L[i]:
      if i+1==A[A[i][0]-1][0]:
        P[i]=1
        sw=1
  #print(P)
  if sw:
    for i in range(N):
      if P[i]:
        A[i].popleft()
        L[i]-=1
    ans+=1
  else:
    tmp=0
    for a in A:
      tmp+=len(a)
    if tmp!=0:
      ans=-1
    break
print(ans)
    

### FIXED CODE ###

import sys
input = sys.stdin.buffer.readline

N = int(input())
A = [[]] + [list(map(int, input().split()))[::-1] for _ in range(N)]

pair = [[0 for _ in range(N + 1)] for _ in range(N + 1)]

stack = []
for i in range(1, N + 1):
  a = A[i].pop()
  pair[i][a] += 1
  if pair[i][a] == pair[a][i] == 1:
    stack.append(i)
    stack.append(a)

r = []
answer = 1
while stack:
  while stack:
    i = stack.pop()
    if A[i]:
      a = A[i].pop()
      pair[i][a] += 1
      if pair[i][a] == pair[a][i] == 1:
        r.append(i)
        r.append(a)
  stack = r
  if r:
    answer += 1
  r = []
  
flag = 1
for i in range(1, N + 1):
  if A[i]:
    flag = 0
    break
if flag:
  print(answer)
else:
  print(-1)
### BUGGY CODE ###

def getval():
    h,w = map(int,input().split())
    a = [list(map(int,input().split())) for i in range(h)]
    b = [list(map(int,input().split())) for i in range(h)]
    return h,w,a,b

def main(h,w,a,b):
    dp = [[False for i in range(160*80+1)] for i in range(w)]
    dp[0][abs(a[0][0]-b[0][0])] = True
    for i in range(w-1):
        cura = a[0][i+1]
        curb = b[0][i+1]
        prev = dp[i]
        for j in range(160*80+1):
            if prev[j]:
                u1 = abs(j-(cura-curb))
                u2 = abs(j-(curb-cura))
                for k in [u1,u2]:
                    if k<=160*80:
                        dp[i+1][k] = True
    #for i in dp:
      #print(i[:20])
    for i in range(h-1):
        temp = [[False for i in range(160*80+1)] for i in range(w)]
        for j in range(w):
            prev1 = dp[j]
            prev2 = temp[j-1]
            cura = a[i+1][j]
            curb = b[i+1][j]
            for k in range(160*80+1):
                if prev1[k] or prev2[k]:
                    u1 = abs(k-(cura-curb))
                    u2 = abs(k-(curb-cura))
                    for l in [u1,u2]:
                        if l<160*80:
                            temp[j][l] = True
        dp = temp
        #for i in dp:
          #print(i[:20])

    ans = 0
    for i in range(160*80+1):
        if dp[-1][i]:
            ans = i 
            break
    print(ans)        

if __name__=="__main__":
    h,w,a,b = getval()
    main(h,w,a,b) 

### FIXED CODE ###

def getval():
    h,w = map(int,input().split())
    a = [list(map(int,input().split())) for i in range(h)]
    b = [list(map(int,input().split())) for i in range(h)]
    return h,w,a,b

def main(h,w,a,b):
    dp = []
    for i in range(w):
        dp.append([False for j in range(1+160*(i+1))])

    dp[0][abs(a[0][0]-b[0][0])] = True
    for i in range(w-1):
        cura = a[0][i+1]
        curb = b[0][i+1]
        prev = dp[i]
        for j in range(len(prev)):
            if prev[j]:
                u1 = abs(j-(cura-curb))
                u2 = abs(j-(curb-cura))
                for k in [u1,u2]:
                    if k<=160*80:
                        dp[i+1][k] = True
    #for i in dp:
      #print(i[:20])
    for i in range(h-1):
        temp = []
        rowa = a[i+1]
        rowb = b[i+1]
        for j in range(w):
            cur = [False for k in range(min(1+160*(i+j+2),1+160*80))]
            prev1 = dp[j]
            prev2 = dp[j]
            if temp:
                prev2 = temp[-1]
            cura = rowa[j]
            curb = rowb[j]
            for k in range(len(prev1)):
                if prev1[k] or prev2[k]:
                    u1 = abs(k-(cura-curb))
                    u2 = abs(k-(curb-cura))
                    if u1<len(cur):
                        cur[u1] = True
                    if u2<len(cur):
                        cur[u2] = True
            temp.append(cur)
        dp = temp
        #for i in dp:
          #print(i[:20])

    ans = 0
    for i in range(160*80+1):
        if dp[-1][i]:
            ans = i 
            break
    print(ans)        

if __name__=="__main__":
    h,w,a,b = getval()
    main(h,w,a,b)
### BUGGY CODE ###

def main():
    import sys
    from collections import Counter

    input = sys.stdin.readline

    s = int(input())
    li = []
    li_append = li.append

    cm = {}
    for i in range(1,2*(10 ** 5)):
        cm[i] = i * (i - 1) // 2
    cm[0] = 0



    for i in range(1, len(str(s))+1):
        num = s % (10 ** i)
        li_append(int(num) % 2019)

    co = Counter(li)
    co_1 = Counter(co.values())
    ans_1 = 0

    for x in co_1.keys():
        if x != 1:
            ans_1 += cm[x]* co_1[x]
    ans2 = 0
    if 0 in co:
        ans2 = co[0]
    print(ans_1 + ans2)

if __name__ == "__main__":
    main()

### FIXED CODE ###

def main():
    import sys
    from collections import defaultdict
    dic = defaultdict(int)

    input = sys.stdin.readline

    s = str(input().rstrip())
    m = 0
    before_m = 0.1

    for i in range(1, len(s) + 1):
        before_m = int(before_m*10)%2019
        m_ad = (int(s[-i]) * before_m) % 2019
        m = (m_ad + m) % 2019
        dic[m] += 1

    ans1 = 0

    for x in dic.values():
        if x > 1:
            ans1 += x * (x - 1) // 2
    ans2 = dic[0]

    print(ans1 + ans2)

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

n = int(input())
a = [input() for _ in range(n)]
a_dict = {i:0 for i in set(a)}

for i in a:
    for j in a_dict:
        if i == j:
            a_dict[j] += 1

ans = []
for key, value in a_dict.items():
    if value == max(a_dict.values()):
        ans.append(key)

ans.sort()
for i in ans:
    print(i)

### FIXED CODE ###

n = int(input())

# 入力データ受取り時に、辞書に追加していくやり方
s_dict = {}
for i in range(n):
    s = input()    
    if s not in s_dict:
        s_dict[s] = 1
    s_dict[s] += 1

max_value = max(s_dict.values())
ans = []
for key, value in s_dict.items():
    if value == max_value:
        ans.append(key)

ans.sort()
for i in ans:
    print(i)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n = int(input())
a = list( map(int, input().split()))

from collections import defaultdict
ar = defaultdict(list)
al = defaultdict(list)

for i in range(n):
    al[i+a[i]].append(i)
    ar[i-a[i]].append(i)

mi = 0-max(a)
ma = n+max(a)

ans = 0
for i in range(mi,ma):
    ans += len(ar[i]) * len(al[i])

print(ans)




### FIXED CODE ###

import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

n = int(input())
a = list( map(int, input().split()))

from collections import defaultdict
ar = defaultdict(list)
al = defaultdict(list)

for i in range(n):
    al[i+a[i]].append(i)
    ar[i-a[i]].append(i)

x = al.keys()

ans = 0
for i in x:
    ans += len(ar[i]) * len(al[i])

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

n = int(input())
keta = len(str(n))
s = 0
base = 10**(keta-1)
while base > 0:
    r,n = divmod(n,base)
    s += r
    base //= 10
#print(s)
if s % 9 == 0:
    print('Yes')
else:
    print('No')



### FIXED CODE ###

n = int(input())
if n % 9 == 0:
    print('Yes')
else:
    print('No')
### BUGGY CODE ###

n=int(input())
s=input()
tot=0

for i in range(n-2):
  for j in range(i+1,n-1):
    for k in range(j+1,n):
      if k-j!=j-i:
        if s[i]!=s[j] and s[j]!=s[k] and s[k]!=s[i]:
          tot+=1
          
print(str(tot))

### FIXED CODE ###

n=int(input())
s=input()
r=0
g=0
b=0

for i in range(n):
  if s[i]=="R":
    r+=1
  elif s[i]=="G":
    g+=1
  else:
    b+=1
    
ans=r*g*b

for i in range(n-2):
  for j in range(i+1,i+1+(n-i-1)//2):
    if s[i]==s[j]:
      continue
    elif s[i]!=s[2*j-i] and s[j]!=s[2*j-i]:
      ans-=1
      
print(str(ans))
### BUGGY CODE ###

N =int(input())
List = []
Dictionary = []
for i in range(N):
    In = input()
    List.append(In)
    if In in Dictionary:
        continue
    else:
        Dictionary.append(In)
Max = 0
Answer = []
for j in range(len(Dictionary)):
    var = List.count(Dictionary[j])
    if var > Max:
        Max = var
        Answer = []
        Answer.append(Dictionary[j])
    elif var == Max:
        Answer.append(Dictionary[j])
Answer.sort()
for _ in range(len(Answer)):
    print(Answer[_])

### FIXED CODE ###

import collections
import itertools


N =int(input())
List = []
Dictionary = []
for i in range(N):
    In = input()
    List.append(In)
    
counters= collections.Counter(List)
counter = sorted(counters.items())
mode_v = counters.most_common()[0][-1]

for j in range(len(counter)):
    an = counter[j][1]
    if mode_v == an:
        print(counter[j][0])
    else:
        continue
### BUGGY CODE ###

K, S = map(int, input().split())

ans = 0

for i in range(0,K+1):
    for j in range (0,K+1):
        for k in range (0,K+1):
            if (i + j + k) == S:
                ans = ans + 1
            

print(ans)


### FIXED CODE ###

K, S = map(int, input().split())

ans = 0

for i in range(0,K+1):
    for j in range (0,K+1):
        if (S-K) <= (i + j) <= S:
            ans = ans + 1
            continue
            

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

#coding:utf-8
import math
import itertools
from functools import reduce

def gcd(num):
    return reduce(math.gcd, num)


if __name__ == "__main__":

    k = int(input())
    ans = 0

    num = list(range(1, k+1))
    comb = tuple(itertools.product(num, repeat=3))


    for i in range(len(comb)):
        ans += gcd(comb[i])

    print(ans)


### FIXED CODE ###

#coding:utf-8
from math import gcd

def main():
    k = int(input())
    ans = 0

    for a in range(1, k+1):
        for b in range(1, k+1):
            for c in range(1, k+1):
                ans += gcd(gcd(a, b), c)

    print(ans)

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

n = int(input())
numbers = list(map(int, input().split(' ')))
counter = 0

for i in range(n):
    flag = True
    for j in range(i):
        if numbers[i] > numbers[j]:
            flag = False
    if flag is True:
        counter += 1
print(counter)

### FIXED CODE ###

n = int(input())
numbers = list(map(int, input().split(' ')))
counter = 0
min_value = numbers[0]
for number in numbers:
    if number <= min_value:
        counter += 1
        min_value = number

print(counter)
### BUGGY CODE ###

x = int(input())

if x == 1:
    print("1")
    exit()


def func(p):
    q = p
    while q < x:
        q *= p
        if q == x:
            return q
            break

    if q / p == p:
        return 1
    else:
        return q / p


z = []
a = 1

while a <= x:
    z.append(func(a))
    a += 1

print(max(z))


### FIXED CODE ###

x = int(input())

if x == 1:
    print("1")
    exit()


def func(p):
    q = p
    while q < x:
        q *= p
        if q == x:
            return q
    if q / p == p:
        return 1
    else:
        return q // p


z = []
a = 2

while a <= x:
    z.append(func(a))
    a += 1

print(max(z))
### BUGGY CODE ###

H, W = map(int, input().split())
masu = []
for i in range(H):
    masu.append(list(input()))
white = []
black = []
for i in range(H):
    for j in range(W):
        if masu[i][j] == ".":
            white.append([i,j])
        else:
            black.append([i,j])
ans = -1
while len(black) != 0:
    ans += 1
    blacktemp = black[:]
    black = []
    for k in blacktemp:
        for i, j in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
            if 0 <= k[0] + i <= H-1 and 0 <= k[1] + j <= W-1:
                if masu[k[0]+i][k[1]+j] == ".":
                    masu[k[0]+i][k[1]+j] = "#"
                    black.append([k[0]+i, k[1]+j])
print(ans)


### FIXED CODE ###

H, W = map(int, input().split())
masu = []
for i in range(H):
    masu.append(list(input()))
black = []
for i in range(H):
    for j in range(W):
        if masu[i][j] == "#":
            black.append([i,j])
ans = -1
while len(black) != 0:
    ans += 1
    blacktemp = black[:]
    black = []
    for k in blacktemp:
        for i, j in [[0, -1], [0, 1], [-1, 0], [1, 0]]:
            if 0 <= k[0] + i <= H-1 and 0 <= k[1] + j <= W-1:
                if masu[k[0]+i][k[1]+j] == ".":
                    masu[k[0]+i][k[1]+j] = "#"
                    black.append([k[0]+i, k[1]+j])
print(ans)
### BUGGY CODE ###

import sys
from collections import Counter
n, p = [int(i) for i in sys.stdin.readline().split()]
s = sys.stdin.readline().strip()
if p == 2:
    ans = 0
    for i, _s in enumerate(s, 1):
        if int(_s) % 2 == 0:
            ans += i
    print(ans)
elif p == 5:
    ans = 0
    for i, _s in enumerate(s, 1):
        if int(_s) in {0, 5}:
            ans += i
    print(ans)
else:
    us = Counter([])
    cur = ""
    ans = 0
    mod = 0
    for i, _s in enumerate(s[::-1]):
        mod = (10 ** i % p * int(_s) + mod) % p
        if mod == 0:
            ans += us[mod] + 1
        else:
            ans += us[mod]
        us.update([mod])
    print(ans)

### FIXED CODE ###

import sys
from collections import Counter
n, p = [int(i) for i in sys.stdin.readline().split()]
s = sys.stdin.readline().strip()
if p == 2:
    ans = 0
    for i, _s in enumerate(s, 1):
        if int(_s) % 2 == 0:
            ans += i
    print(ans)
elif p == 5:
    ans = 0
    for i, _s in enumerate(s, 1):
        if int(_s) in {0, 5}:
            ans += i
    print(ans)
else:
    us = Counter([])
    cur = ""
    ans = 0
    mod = 0
    keta_mod = 1
    for i, _s in enumerate(s[::-1]):
        keta_mod = keta_mod * 10 % p
        mod = (keta_mod * int(_s) + mod) % p
        if mod == 0:
            ans += us[mod] + 1
        else:
            ans += us[mod]
        us.update([mod])
    print(ans)
### BUGGY CODE ###

N = int(input())
count = 0
clip = []
for i in range(N):
    S = input()
    if S in clip:
        pass
    else:
        clip.append(S)
        count = count + 1
    
print(count)

### FIXED CODE ###

N = int(input())

clip = set()
for i in range(N):
    S = input()
    clip.add(S)

    
print(len(clip))
### BUGGY CODE ###

n = int(input())
l = list(map(int, input().split()))
l = sorted(l)
import bisect
ans = 0
for i in range(n):
  a = l[i]
  for j in range(i + 1, n):
    b = l[j]
    bel = bisect.bisect_left(l, b+a, lo = j + 1)
    ans += (bel-j-1)
print(ans)

### FIXED CODE ###

import bisect
n = int(input())
l = sorted(list(map(int,input().split())))
ans = 0
 
for i in range(n-2):
    for j in range(i+1, n-1):
        ans += bisect.bisect_left(l,l[i] + l[j]) - j - 1
print(ans)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

h,w,m = readints()

r = [0]*h
c = [0]*w

d = [set() for i in range(h)]
for i in range(m):
    a,b = (x-1 for x in readints())
    r[a] += 1
    c[b] += 1
    d[a].add(b)

R = [i for i,x in enumerate(r) if x==max(r)]
C = [i for i,x in enumerate(c) if x==max(c)]

flag = 1
if len(R)*len(C)>m:
    flag = 0
else:
    for x in R:
        for y in C:
            if y not in d[x]:
                flag = 0
                break

print(max(r)+max(c)-flag)






### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

import bisect
from operator import itemgetter

h,w,m = readints()

r = [0]*h
c = [0]*w

p = [readints() for i in range(m)]
p.sort(key=itemgetter(1))

d = [[] for i in range(h)]
for i in range(m):
    a,b = (x-1 for x in p[i])
    r[a] += 1
    c[b] += 1
    d[a].append(b)

mr = max(r)
mc = max(c)
R = [i for i,x in enumerate(r) if x==mr]
C = [i for i,x in enumerate(c) if x==mc]

flag = 1
if len(R)*len(C)>m:
    flag = 0
else:
    for x in R:
        for y in C:
            z = bisect.bisect_left(d[x],y)
            if z>=len(d[x]):
                flag = 0
                break
            else:
                if y != d[x][z]:
                    flag = 0
                    break

print(max(r)+max(c)-flag)
### BUGGY CODE ###

from collections import Counter

n = int(input())
a = input().split()
add=''
for k in range(n):
    if(add!=''):
        a.insert(k-1,add)
    add=a.pop(k)
  
    freq = Counter(a)
    total = 0
  
    for key,val in freq.items():
        if( val>1):
            total+= (val*(val-1))//2
        
    print(total)

### FIXED CODE ###

from collections import Counter

n = int(input())
a = input().split()
freq = Counter(a)
#print(freq)
total=0
    
for key,val in freq.items():
    total+=(val*(val-1))//2

for k in range(n): 

    kth = (freq[a[k]]*(freq[a[k]]-1))//2
    newkth = ((freq[a[k]]-2)*(freq[a[k]]-1))//2  

    total -= kth
    total += newkth
    
    print(total)
    
    total -=newkth
    total +=kth
### BUGGY CODE ###

n=int(input())
import fractions 
from functools import reduce
arr = list(map(int,input().split()))

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcma(numbers):
    return reduce(lcm_base, numbers, 1)
 
a = lcma(arr)
ans = [(a//i)%1000000007 for i in arr]
cnt=0
for i in ans:cnt = cnt+ i %1000000007
print(cnt%1000000007)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
mod = 10**9+7
 
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)
 
a = 1
for i in range(N):
    a = lcm(a, A[i])
 
ans = 0
for i in range(N):
    ans += a * pow(A[i], mod-2, mod)
 
print(ans % mod)
### BUGGY CODE ###

# -*- coding: utf-8 -*-
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
import copy
import math
sys.setrecursionlimit(10**6)

# lis_of_lis = [[] for _ in range(N)]


def zz(): return list(map(int, sys.stdin.readline().split()))


def z(): return int(sys.stdin.readline())


def S(): return sys.stdin.readline()[:-1]


def C(line): return [sys.stdin.readline() for _ in range(line)]


s = S()
t = S()
set_s = set(s)
set_t = set(t)
if (set_t <= set_s) is False:
    print(-1)
    exit()
alphabet2index = defaultdict(list)
for i, alphabet in enumerate(s):
    alphabet2index[alphabet].append(i+1)
ans = 0
pos = 0
prev_pos = 0
base = 0
n = -1
c = 0
for _t in t:
    n = s.find(_t, n+1)
    if (n == -1):
        c += 1
        n = s.find(_t)
ans = n + c*len(s) + 1
"""

    ng_flg = True
    while ng_flg:
        pos_lis = alphabet2index[_t]
        i = 0
        while len(pos_lis) > i:
            if (pos_lis[i] >= pos):
                pos = pos_lis[i]
                ans += (pos_lis[i]-prev_pos)
                # print(pos_lis[i], prev_pos, )
                prev_pos = pos
                ng_flg = False
                break
            i += 1
        else:
            prev_pos = pos - len(s)
            # print(prev_pos)
            # print('base', base)
            pos = 0
    """
print(ans)


### FIXED CODE ###

# -*- coding: utf-8 -*-
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
import copy
import math
sys.setrecursionlimit(10**6)

# lis_of_lis = [[] for _ in range(N)]


def zz(): return list(map(int, sys.stdin.readline().split()))


def z(): return int(sys.stdin.readline())


def S(): return sys.stdin.readline()[:-1]


def C(line): return [sys.stdin.readline() for _ in range(line)]


s = S()
t = S()
set_s = set(s)
set_t = set(t)
if (set_t <= set_s) is False:
    print(-1)
    exit()
alphabet2index = defaultdict(list)
for i, alphabet in enumerate(s):
    alphabet2index[alphabet].append(i+1)
ans = 0
pos = 0
prev_pos = 0
base = 0
n = -1
c = 0
for _t in t:
    n = s.find(_t, n+1)
    if (n == -1):
        c += 1
        n = s.find(_t)
ans = n + c*len(s) + 1
"""

    ng_flg = True
    while ng_flg:
        pos_lis = alphabet2index[_t]
        i = 0
        while len(pos_lis) > i:
            if (pos_lis[i] >= pos):
                pos = pos_lis[i]
                ans += (pos_lis[i]-prev_pos)
                # print(pos_lis[i], prev_pos, )
                prev_pos = pos
                ng_flg = False
                break
            i += 1
        else:
            prev_pos = pos - len(s)
            # print(prev_pos)
            # print('base', base)
            pos = 0
    """
print(ans)
### BUGGY CODE ###

s = input()

i = 0
while(True):
    if ("01" in s ) :
        s = s.replace("01","",1) 
        i += 1
    elif "10" in s:
        s = s.replace("10","",1)
        i += 1
    else:
        break
print(i*2)

### FIXED CODE ###

s = input()

num_0 = s.count("0")
num_1 = s.count("1")
if num_0 < num_1:
    print(num_0*2)
else:
    print(num_1*2)