Harryxun/stf_run huggingface dataset:
n=int(raw_input())
p=map(lambda x:int(x)-1,raw_input().split())
a=range(1,n+1)
b=range(n,0,-1)
for i in xrange(1,n):
    if p[i]>p[i-1]:
        diff=abs(a[p[i]]+b[p[i]]-(a[p[i-1]]+b[p[i-1]]))
        for j in xrange(p[i],n):
            a[j]=a[j]+diff+1
    else:
        diff=abs(a[p[i]]+b[p[i]]-(a[p[i-1]]+b[p[i-1]]))
        for j in xrange(p[i],-1,-1):
            b[j]=b[j]+diff+1
print(" ".join(map(str,a)))
print(" ".join(map(str,b)))

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)

def check(y,x):
    if 0<=y<=h-1 and 0<=x<=w-1:
        if g[y][x]=="#":
            return True
    return False

def dfs(y,x):
    dx=(0,1)
    dy=(1,0)
    if y==h-1 and x==w-1:
        return True
    for i in xrange(2):
        nxy=y+dy[i]
        nxx=x+dx[i]
        if check(nxy,nxx) and not visited[nxy][nxx]:
            visited[nxy][nxx]=True
            if dfs(nxy,nxx):
                return True
            visited[nxy][nxx]=False
    return False

h,w=map(int,raw_input().split())
g=[raw_input() for _ in xrange(h)]
visited=[[False]*w for _ in xrange(h)]
visited[0][0]=True
if not dfs(0,0):
    print("Impossible")
    exit()
for i in xrange(h):
    for j in xrange(w):
        if g[i][j]=="#" and not visited[i][j]:
            print("Impossible")
            exit()
print("Possible")
N = int(input())
A = input().split()
Q = int(input())
for q in range(Q):
  b, c = input().split()
  A = A.replace(b,c)
  An = list(map(int,A)
  print(sum(An))

### FIXED CODE ###

from collections import Counter
N = int(input())
A = list(map(int,input().split()))
C_A = Counter(A)
S = 0
for ca in C_A:
  S += ca * C_A[ca]

Q = int(input())
for q in range(Q):
  b, c = map(int,input().split())
  S += (c - b) * C_A[b]
  C_A[c] += C_A[b]
  C_A[b] = 0
  
  print(S)
N,A,B = map(int,input().split)
if N*A>=B:
    print(N*A)
else:
    print(B)
    

### FIXED CODE ###

N,A,B = map(int,input().split())
if N*A>=B:
    print(B)
else:
    print(N*A)
n=list(input())
n_list=[int(i) for i in n]
if n_list[0]!=1 and set(n_list)!=[9]:
  ans=9*(len(n_list)-1)+n_list[0]-1
  print(ans)
elif n_list[0]!=1 and set(n_list)==[9]:
  print(9*len(n_list))
elif n_list[0]+n_list[1]<=9:
  ans=(len(n_list)-1)*9
  print(ans)
else:
  ans=n_list[0]+n_list[1]-1+9*(len(n_list)-2)
  print(ans)
  
  



### FIXED CODE ###

N = input()
ans = 0

if N[1:] == "9" * (len(N) - 1):
    ans = int(N[0]) + (9 * (len(N) - 1))
else:
    ans = int(N[0]) + 9 * (len(N) - 1) - 1

print(ans)
def check(N, K):
    # k < N-2
    if K < N - 2:
        print(-1)
        return
    # k == N -2
    if K == N - 2:
        print(N-1)
        for i in range(1, N):
            print(i, i+1)
        return
    # N -1 <= k <= (N - 1)*(N-2) /2
    if N-1 <= K and K < (N - 1)*(N-2) /2:
        k = (N - 1)*(N-2) /2 - K
        print(N-1 + k)
        for i in range(2, N+1):
            print(1, i)
        l = 2
        r = 3
        for i in range(k):
            print(l, r)
            r+=1
            if r == N+2:
                l+=1
                r=l+1
    # K = (N - 1)*(N-2) /2
    if (N - 1)*(N-2) /2 == K:
        print(-1)
        for i in range(2, N+1):
            print(1, i)
        return
    #  (N - 1)*(N-2) /2 < k
    if (N - 1)*(N - 2) /2 < K:
        print(-1)
        return


if __name__ == "__main__":
    N, K = map(int, input().split())
    check(N, K)


### FIXED CODE ###

def check(N, K):
    if K <= (N - 1)*(N-2) /2:
        k = int((N - 1)*(N-2)/2 - K)
        print(N-1 + k)
        for i in range(2, N+1):
            print(1, i)
        l = 2
        r = 3
        if k != 0:
            for i in range(k):
                if l > N or r > N:
                    break
                print(l, r)
                r+=1
                if r == N+1:
                    l+=1
                    r=l+1
        return
    else:
        print(-1)
        return


if __name__ == "__main__":
    N, K = map(int, input().split())
    check(N, K)
20 20
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................
....................

### FIXED CODE ###

def list2d(a, b, c): return [[c] * b for i in range(a)]
mod =10**9+7
H, W = map(int, input().split())

grid = []

for _ in range(H):
    grid.append(list(input()))

dp = list2d(H, W, 0)
for i in range(H-1, -1, -1):
    if(grid[i][W-1] == '#'):
        break
    dp[i][W-1] = 1

for j in range(W-1, -1, -1):
    if(grid[H-1][j]=='#'):
        break
    dp[H-1][j] = 1

for row in range(H-2, -1, -1):
    for col in range(W-2, -1, -1):
        if(grid[row][col] == '.'):
            dp[row][col] = (dp[row][col+1] + dp[row+1][col])%mod
        else:
            dp[row][col] = 0
print(dp[0][0]%mod)
from sys import stdin
ma = 10**18
n = int(stdin.readline().split())
a = map(int,stdin.readline().split())
x = 0
s = sum(n)
for i in a[:-1]:
 x+= i
 ma  = min(ma, abs(s-2*x) )
print ma

### FIXED CODE ###

from sys import stdin
ma = 10**18
n = int(stdin.readline())
a = map(int,stdin.readline().split())
x = 0
s = sum(a)
for i in a[:-1]:
 x+= i
 ma  = min(ma, abs(s-2*x) )
print ma
n = int(raw_input())

li = []
for i in range(n):
    cmd = raw_input().split()
    if cmd[0] == 'insert':
        li.insert(0,cmd[1])
    elif cmd[0] == 'delete':
        li.remove(cmd[1])
    elif cmd[0] == 'deleteFirst':
        li.pop(0)
    elif cmd[0] == 'deleteLast':
        li.pop()
for e in li:
    print e,

### FIXED CODE ###

import sys,gc
    
A=[]
fp =0
bp =0

n=int(raw_input())
while n:
    s = raw_input()
    if s[0] == 'i':
        A.append(int(s[7:]))
        fp += 1
    elif s[6] == ' ':
        try:
            i = A[::-1].index(int(s[7:]))
            if i!=-1:
                del A[-i-1]
                fp -=1
        except:
            pass
    elif s[6] == 'F':
        A.pop()
        fp -=1
    elif s[6] == 'L':
        bp +=1
    n -=1

for e in A[bp:fp+1][::-1]:
    print int(e),
import random
import time

start = time.time()
D = int(input())
C = list(map(int, input().split()))
S = [list(map(int, input().split())) for _ in range(D)]


def main():
    best_score = 0
    best_ans = [0] * 26
    base = list(range(26))
    while time.time() - start < 1.8:
        random.shuffle(base)
        score = calc_score(base)
        if score > best_score:
            best_score = score
            ans = base[:]

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


def calc_score(T):
    score = 0
    last_day = [-1] * 26
    for d, t in enumerate(T):
        score += S[d][t]
        last_day[t] = d
        for c, ld in zip(C, last_day):
            score -= c * (d - ld)
    return score


if __name__ == "__main__":
    main()


### FIXED CODE ###

def main():
    D = int(input())
    C = list(map(int, input().split()))
    S = [list(map(int, input().split())) for _ in range(D)]

    score = 0
    last_day = [-1] * 26

    for d in range(D):
        print(S[d].index(max(S[d])) + 1)


if __name__ == '__main__':
    main()
N,M = map(int,input().split())
Ls = []
Lp = []
for i in range(N):
    a,b = map(int,input().split())
    Ls.append(a)
    Ls.append(b)
for m in range(M):
    c,d = map(int,input().split())
    Lp.append(c)
    Lp.append(d)

for i in range(N):
    for m in range(M):
        dis = list(abs(Ls[i][0]-Lp[m][0])+abs(Ls[i][1]-Lp[m][1]))
    print(dis.index(min(dis))+1)

### FIXED CODE ###

#ABC057.B
N,M = map(int,input().split())
L1 = []
L2 = []

for i in range(N):
    a,b = map(int,input().split())
    L1.append([a,b])
for m in range(M):
    c,d = map(int,input().split())
    L2.append([c,d])

for j in range(N):
    L3 = []
    for l in range(M):
        L3.append(abs(L1[j][0]-L2[l][0]) + abs(L1[j][1]-L2[l][1]))
    print(L3.index(min(L3))+1)
n, a, b = [int(input()) for i in range(3)]
if n==1:
  print('Hello World')
else:
  print(a+b)
  
  


### FIXED CODE ###

n = int(input())
if n == 1:
    print('Hello World')
else:
    a, b = [int(input()) for i in range(2)]
    print(a+b)
a,b,c=map(int,input().split())
if a==b==5 and c==7:
  print('YES')
elif a==c==5 and b==7:
  print('YES')
elif b==c==5 amd c==7:
  print("YES")
else:
  print('NO')

### FIXED CODE ###

a,b,c=map(int,input().split())
if a==b==5 and c==7:
  print('YES')
elif a==c==5 and b==7:
  print('YES')
elif b==c==5 and a==7:
  print("YES")
else:
  print('NO')
N,T = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(N)]

import sys

time = 0
mintime = 10**9

for shop in range(N):
    time += 1
    for i in range(len(ab)):
        t = time + ab[i][0]*time + ab[i][1]
        if t < mintime:
            mintime = t
            go = i
    mintime = 10**9
    time = time + ab[go][0]*time + ab[go][1]
    
    if time > T+0.5:
        print(shop)
        sys.exit()
    
    ab.pop(go)
    
print(shop+1)

### FIXED CODE ###

import sys
from bisect import bisect_right
 
N,T = map(int,input().split())
ab = [list(map(int,input().split())) for i in range(N)]

b0 = []
ab1 = []

#aが0とそれ以外で分ける
for a,b in ab:
    if a == 0:
        #+1は店間の移動を先に考慮
        b0.append(b+1)
    else:
        ab1.append([(b+1)/a, a, b])

N0 = len(b0)
N1 = len(ab1)

b0.sort()

#bは累積和で時間計算可能（最後にどこまで足せるかを調べる）
if N0>0:
    b_ruiseki = [0]*N0
    b_ruiseki[0] = b0[0]
    for i in range(1,N0):
        b_ruiseki[i] = b_ruiseki[i-1] + b0[i]
else:
    b_ruiseki = []
#探索範囲が28店まででいいからソートする
ab1.sort()

INF = T + 100
 
dp = [[INF]*31 for i in range(N1+1)]
dp[0][0] = 0
ans = 0
for i in range(N1):
    # dp[i+1][j] : 0~i番目までの店を見て，j個選ぶときの最小時間
    for j in range(31):
        if j > i+1:
            break
        if j == 0:
            dp[i+1][j] = 0
        else:
            dp[i+1][j] = min(dp[i][j], dp[i][j-1]+1 + ab1[i][1]*(dp[i][j-1]+1) + ab1[i][2])

for j in range(31):
    if dp[N1][j] > T:
        break    
    # a>0をj店舗めぐった後にa=0を何店舗めぐれるか
    num0 = bisect_right(b_ruiseki, T - dp[N1][j])
    ans = max(ans, j + num0)
        
    
print(ans)
import math
import scipy.optimize

a, b, x = [int(i) for i in input().split()]

def func_a(theta):
    S = 0.5 * b**2 / math.tan(theta)
    V = S * a
    equal = V - x

    return equal

def func_b(theta):
    S = a * b - 0.5 * a**2 * math.tan(theta)
    V = S * a
    equal = V - x

    return equal

if x <= 0.5 * a**2 * b:
    result = scipy.optimize.bisect(func_a, math.pi/4, math.pi/2 - 10**(-6))
else:
    result = scipy.optimize.bisect(func_b, 0 + 10**(-6), math.pi/4)

print(math.degrees(result))


### FIXED CODE ###

import math
a, b, x = [int(i) for i in input().split()]

V_max = a * a * b

if x <= 0.5 * V_max:
    theta = math.atan((a*b*b)/(2*x))
else:
    theta = math.atan(2*(a**2*b - x)/(a**3))

print(math.degrees(theta))
a, b, c = getNM()
print(len({a, b, c}))

### FIXED CODE ###

import sys
import math
from functools import reduce
import bisect


def getN():
    return int(input())


def getNM():
    return map(int, input().split())


def getList():
    return list(map(int, input().split()))


def input():
    return sys.stdin.readline().rstrip()


# input = sys.stdin.buffer.readline

def index(a, x):
    i = bisect.bisect_left(a, x)
    if i != len(a) and a[i] == x:
        return i
    return False


#############
# MAIN CODE #
#############

a, b, c = getNM()
print(len({a, b, c}))
S=input()
if S==AAA or S==BBB:
	print("No")
else:
	print("Yes")

### FIXED CODE ###

S=input()
if S=="AAA" or S=="BBB":
	print("No")
else:
	print("Yes")
import numpy as np
li = np.array([])

N=int(input())
li = list(map(int, input().split()))
arr = np.array(li)

max = 0

for i in range(100000 + 1):

  a = np.count_nonzero(arr == i or arr == (i + 1) or arr == (i - 1))

  if max < a:
    max = a

print(a)

### FIXED CODE ###

n = int(input())
cnt = [0] * 100002
a = list(map(int, input().split()))
for v in a:
    cnt[v] += 1
    cnt[v-1] += 1
    cnt[v+1] += 1
    
print(max(cnt))
def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

N = int(input())
A= list(map(int,input().split()))
output=0

for i in range(N):
        M = [A[i-1]]
        hoge = A[i]
        A[i] = A[i-1]
        for k in range(N):
                M.append(gcd(M[k],A[k]))
                print(M)

        if M[N] >= output:
                output = M[N]
        A[i]=hoge

print(output)

### FIXED CODE ###

def gcd(a,b):
  while b!=0:
    a,b=b,a%b
  return a

N = int(input())
A= list(map(int,input().split()))
output=0
KA=[0 for i in range(N)]
LA=[0 for i in range(N)]
GCD = []

for k in range(1,N):
        LA[k]=gcd(A[k-1],LA[k-1])

for k in range(1,N):
        KA[k]=gcd(A[N-k],KA[k-1])


for k in range(N):
        GCD.append(gcd(LA[k],KA[N-k-1]))
output = max(GCD)
print(output)
#coding:utf-8
x=raw_input()
print( x**3 )

### FIXED CODE ###

import sys

x = int( sys.stdin.readline() )
print( "{}".format( x**3 ) )
a = int(input())
s = input()

if a >= 3200:
  print(s)
 else:
  print('red')

### FIXED CODE ###

a = int(input())
s = input()

if a >= 3200:
  print(s)
else:
  print('red')
n = int(input())
h = list(map(int, input().split()))

count, ans = 0, 0
for i in range(n):
  if h[i] >= h[i+1]:
    count = count + 1
  else:
    ans = max(count, ans)
    count = 0
print(ans)

### FIXED CODE ###

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

count, ans = 0, 0
for i in range(n-1):
  if h[i] >= h[i+1]:
    count = count + 1
    ans = max(count, ans)
  else:
    count = 0
print(ans)
import sys
from bisect import bisect_left
N, M = map(int, input().split())
MOD = 10 ** 9 + 7
NMAX = 10 ** 5

A = []
for _ in range(M):
    A.append(int(input()))

dp = [None for _ in range(N+1)]
dp[0] = 1
dp[1] = 0 if A[0] == 1 else 1
for i in range(2, N+1):
    if i in A:
        dp[i] = 0
    else:
        dp[i] = (dp[i-2] + dp[i-1]) % MOD

print(dp[-1])


### FIXED CODE ###

import sys
N, M = map(int, input().split())
MOD = 10 ** 9 + 7

NG = [False for _ in range(N+1)]
for _ in range(M):
    NG[int(input())] = True

dp = [None for _ in range(N+1)]
dp[0] = 1
dp[1] = 0 if NG[1] else 1
for i in range(2, N+1):
    if NG[i]:
        dp[i] = 0
    else:
        dp[i] = (dp[i-2] + dp[i-1]) % MOD

print(dp[-1])
n = int(input())

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

kind = len(set(a))

if kind % 2 == 0:
  print(kind - 1)
else:
  print(kin)
  

### FIXED CODE ###

n = int(input())

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

kind = len(set(a))

if kind % 2 == 0:
  print(kind - 1)
else:
  print(kind)
from math import gcd # Python3.6以降
# --------------------------------------------------------------

# n以下の素数列挙: O-nlogn
def primes(n):
    prs = []
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]:
            continue
        for j in range(i*2, n+1, i):
            is_prime[j] = False
    for i in range(len(is_prime)):
        if is_prime[i]:
            prs.append(i)
    return prs

n    = int(input())
lis  = list(map(int,input().split()))
soin = [0]*(10**6)
cnt  = 0
ans  = 'pairwise coprime'

for i in range(n):
    soin[lis[i]] += 1

for i in primes(10**6):
    for j in range(1,10**6):
        if i*j >= 10**6:
            break
        cnt += soin[i*j]
        if cnt > 1:
            ans = 'not coprime'
            break

for i in range(n-1):
    if i==0:
        tmp = gcd(lis[i],lis[i+1])
    else:
        tmp = gcd(tmp,lis[i+1])

if tmp==1:
    ans = 'setwise coprime'

print(ans)

### FIXED CODE ###

from math import gcd # Python3.6以降
# --------------------------------------------------------------

# n以下の素数列挙: O-nlogn
def primes(n):
    prs = []
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5)+1):
        if not is_prime[i]:
            continue
        for j in range(i*2, n+1, i):
            is_prime[j] = False
    for i in range(len(is_prime)):
        if is_prime[i]:
            prs.append(i)
    return prs

n    = int(input())
lis  = list(map(int,input().split()))
A    = 10**6
nlis = [0]*A
flg_pairwise = True

for i in range(n):
    nlis[lis[i]-1] += 1

for i in primes(A):
    cnt = 0
    for j in range(i-1,A,i):
        cnt += nlis[j]
    if cnt > 1:
        flg_pairwise = False
        break

if flg_pairwise:
    print('pairwise coprime')
else:
    for i in range(n-1):
        if i==0:
            tmp = gcd(lis[i],lis[i+1])
        else:
            tmp = gcd(tmp,lis[i+1])

    if tmp==1:
        print('setwise coprime')
    else:
        print('not coprime')
n = int(input())
print(sum(list(range(1,n))))

### FIXED CODE ###

n = int(input())
print(n*(n-1)//2)
import math

H,A = map(int, input().split)
float_ver = H / A
print(math.ceil(float_ver))

### FIXED CODE ###

H,A = map(int, input().split())
if H % A == 0:
    print(H // A)
else:
    print(H // A + 1)
A,B,K = map(int,input().split())
if A >= 1000000000000 or B >= 1000000000000 or K >= 1000000000000:
    A = 0
    B = 0
else:
    C = max(0,A-K)
    B = max(0,(A+ B) - K)
print(C,B)

### FIXED CODE ###

a, b, k = map(int, input().split())
print(max(0, a - k), max(0, b - max(0, k - a)))
def f(x):
    p=0
    cnt=0
    for i in range(n-1,-1,-1):
        while p<n and a[i]+a[p]<x:
            p+=1
            if p==n: return cnt
        cnt+=n-p
    return cnt

n,m=map(int,input().split())
a=list(map(int,input().split()))
a.sort()
l,r=a[0]*2,a[-1]*2

while r-l>1:
    md=(r+l)//2
    k=f(md)
    if k==m: break
    elif k>m: l=md
    else: r=md

p=0
cnt=0
ans=0
for i in range(n-1,-1,-1):
    while p<n and a[i]+a[p]<=md:
        p+=1
    cnt+=n-p
    ans+=a[i]*(n-p)*2

ans+=(m-cnt)*md

print(ans)

### FIXED CODE ###

import sys

def f(x):
    p=0
    cnt=0
    for i in range(n-1,-1,-1):
        while p<n and a[i]+a[p]<x:
            p+=1
        cnt+=n-p
    return cnt

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

l,r=a[0]*2,a[n-1]*2
md=(r+l)//2
while r-l>0:
    if r-l<=1:
        if f(r)==m: md=r;break 
        else: md=l;break
    md=(r+l)//2
    k=f(md)
    if k==m: break
    elif k>m: l=md
    else: r=md

p=0
cnt=0
ans=0

for q in range(n-1,-1,-1):
    try:
        while p<n and a[q]+a[p]<=md:
            p+=1
    except:
        b=1
        while True:
            b+=1

    if p==n: break
    cnt+=n-p
    ans+=a[q]*(n-p)*2


ans+=(m-cnt)*md
print(ans)
S = list(input())
print(S.count("1")

### FIXED CODE ###

s = list(input())
print(s.count("1"))
input(X)

if X>=30:
  print('Yes')
else:
  print('No")
        

### FIXED CODE ###

X = int(input())

if X>=30:
  print('Yes')
else:
  print('No')
s = input() ; c = 0 ; l = []

for i in s:
    if i == 'A' or i == 'T' or i == 'G' or i == 'C':
        c += 1
    if i != 'A' and i != 'T' and i != 'G' and i != 'C':
        l.append(c)
        c = 0
        
print(max(l))
            

### FIXED CODE ###

s = input()
a = ['A','T','G','C']
ans = []
c = 0

for i in s:
    if i in a:
        c += 1
    else:
        ans += [c]
        c = 0
ans += [c]
if ans == []:
    print(len(s))
else:
    print(max(ans))
import networkx as nx
import itertools 

def majic_graph(c):
	graph = nx.DiGraph()
	graph.add_nodes_from(range(10))
	for s,t in itertools.product(range(10), range(10)):
		if s != t:
			graph.add_edge(s,t, weight=c[s][t])
	return graph

def wall(A, c):
	majic = majic_graph(c)
	cost = 0
	for i,j in itertools.product(range(H), range(W)):
		if A[i][j] == -1 or A[i][j] == 1:
			pass
		else:
			cost += nx.dijkstra_path_length(majic, A[i][j], 1)
	print(cost)

items = list(map(int, input().split()))
H = items[0]
W = items[1]
c = list()
A = list()
for i in range(10):
	c_i = list(map(int, input().split()))
	c.append(c_i)
for i in range(H):
	A_i = list(map(int, input().split()))
	A.append(A_i)

wall(A, c)

### FIXED CODE ###

import itertools 

def wall(A,c,H,W):
    for k in range(10):
        for i,j in itertools.product(range(10), range(10)):
            c[i][j] = min(c[i][j], c[i][k]+c[k][j])

    cost = 0
    for i,j in itertools.product(range(H), range(W)):
        if A[i][j] != -1 and A[i][j] != 1:
            cost += c[A[i][j]][1]
    print(cost)


items = list(map(int, input().split()))
H = items[0]
W = items[1]
c = list()
A = list()
for i in range(10):
	c_i = list(map(int, input().split()))
	c.append(c_i)
for i in range(H):
	A_i = list(map(int, input().split()))
	A.append(A_i)
wall(A,c,H,W)
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;

int main() {
  char c;
  cin >> c;
  cout << char(c+1) << endl;
  return 0;
}

### FIXED CODE ###

c = input()
alpha_list = 'abcdefghijkfmnopqrstuvrxyz'

for i in range(len(alpha_list)):
  if c == alpha_list[i]:
    print(alpha_list[i+1])
    break
s = input()

n = int(s)
lst = list(map(int, list(s)))
lst_uniq = list(set(lst))
lst_ans = [9] * (len(lst) - 1)

if lst_uniq == [9] or len(n) == 1:
    ans = sum(lst)
elif lst[0] == 1:
    ans = sum(lst_ans)
else:
    lst_ans = [lst[0] - 1] + lst_ans
    ans = sum(lst_ans)

print(ans)


### FIXED CODE ###

n = input()

lst = list(map(int, list(n)))
print(max(sum(lst), lst[0] - 1 + 9 * (len(lst) - 1)))
tmp = raw_input()
s = list(set(map(int, raw_input().split())))
tmp = raw_input()
t = sorted(map(int, raw_input().split()))

max_n = s[-1]
min_n = s[0]
ans = 0

for i in t:
	if i > max_n or i < min_n: continue
	for j in range(len(s)):
		if i == s[j]:
			s = s[index(j):]
			ans += 1
			break
		elif i <= s[j]:
			break
print ans

### FIXED CODE ###

input()
l = raw_input().split()
input()
print len(set(l) & set(raw_input().split()))
new=S.split('2017')


### FIXED CODE ###

s=input()
s=s[:3]+"8"+s[4:]
print(s)
aa, bb, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

couponMin = float("INF")
for i in range(m):
    x, y, c = map(int, input().split())
    cc = a[x - 1] + b[x - 1] - c
    couponMin = min(couponMin, cc)

print(min(min(a) + min(b), couponMin))




### FIXED CODE ###

aa, bb, m = map(int, input().split())
a = list(map(int, input().split()))
b = list(map(int, input().split()))

couponMin = float("INF")
for i in range(m):
    x, y, c = map(int, input().split())
    cc = a[x - 1] + b[y - 1] - c
    couponMin = min(couponMin, cc)

print(min(min(a) + min(b), couponMin))
if 7 in int(input()):
  print('yes')
else:
  print('no')

### FIXED CODE ###

if "7" in input():
  print("Yes")
else:
  print("No")
A,B=map(int,input().split())
a_line=(A-1)//50+1
b_line=(B-1)//50+1
 
print(2*a_line+2*b_line,99)
for i in range(a_line):
 if i == a_line-1:
  print(('.#'*((A-1)%50)+'#'*(99-2*((A-1)%50))))
 else:
  print(('.#'*49)+'.')
 print('#'*99)
 
for i in range(b_line):
 print('.'*99)
 if i == b_line-1:
  print(('#.'*((B-1)%50)+'.'*(99-2*((B-1)%50))))
 else:
  print(('#.'*49)+',')
A,B=map(int,input().split())
a_line=(A-1)//50+1
b_line=(B-1)//50+1

print(2*a_line+2*b_line,99)
for i in range(a_line):
 if i == a_line-1:
  print(('.#'*((A-1)%50)+'#'*(99-2*((A-1)%50))))
 else:
  print(('.#'*49)+'.')
 print('#'*99)

for i in range(b_line):
 print('.'*99)
 if i == b_line-1:
  print(('#.'*((B-1)%50)+'.'*(99-2*((B-1)%50))))
 else:
  print(('#.'*49)+'.')


### FIXED CODE ###

A,B=map(int,input().split())
a_line=(A-1)//50+1
b_line=(B-1)//50+1

print(2*a_line+2*b_line,99)
for i in range(a_line):
 if i == a_line-1:
  print(('.#'*((A-1)%50)+'#'*(99-2*((A-1)%50))))
 else:
  print(('.#'*49)+'.')
 print('#'*99)

for i in range(b_line):
 print('.'*99)
 if i == b_line-1:
  print(('#.'*((B-1)%50)+'.'*(99-2*((B-1)%50))))
 else:
  print(('#.'*49)+'#')
o = list(input())
e = list(input())
ans = []

for i in range(len(o)):
    ans += [o[i],e[i]]
print(*ans,sep="")

### FIXED CODE ###

o = list(input())
e = list(input())
ans = []

if len(o) != len(e):
    e += [""]

for i in range(len(o)):
    ans += [o[i],e[i]]
print(*ans,sep="")
N = int(input())
s = []
for i in range(N):
	s.append(input())
c = 0
for j in len(s):
	c += s[i].count("AB")
a = 0
b = 0
for k in len(s):
	if s[i][-1] == "A":
    	a += 1
for l in len(s):
	if s[i][0] == "B":
    	b += 1
print(c+Math.min(a,b))

### FIXED CODE ###

N = int(input())
S = [input() for _ in range(N)]
ans = 0
ba = b = a = 0
for s in S:
    ans += s.count('AB')
    if s[0] + s[-1] == 'BA':
        ba += 1
    if s[0] == 'B':
        b += 1
    if s[-1] == 'A':
        a += 1
ans += min(a, b)
if a == b == ba and b:
    ans -= 1
print(ans)