Harryxun/stf_run huggingface dataset:
import sys
readline = sys.stdin.readline
def MAIN():
    n, m = map(int, input().split())
    li = [i for i in range(n)]
    def f(a):
        if li[a] == a:
            return a
        li[a] = f(li[a])
        return li[a]
    def f2(a, b):
        if li[a] == a:
            li[a] = b
            return
        f2(li[a], b)
        li[a] = b
    for _ in range(m):
        x, y = map(int, readline().split())
        if f(x) > f(y):
            f2(x, li[y])
        elif f(x) < f(y):
            f2(y, li[x])
    for _ in range(int(input())):
        x, y = map(int, readline().split())
		print("yes" if f(a) == f(b) else "no")
MAIN()



### FIXED CODE ###

import sys
readline = sys.stdin.readline
def MAIN():
    n, m = map(int, input().split())
    li = [i for i in range(n)]
    def f(a):
        if li[a] == a:
            return a
        li[a] = f(li[a])
        return li[a]
    def f2(a, b):
        if li[a] == a:
            li[a] = b
        else:
            f2(li[a], b)
            li[a] = b
    def solve(a, b):
        if f(a) == f(b):
            return "yes"
        return "no"
    for _ in range(m):
        x, y = map(int, readline().split())
        if f(x) > f(y):
            f2(x, li[y])
        else:
            f2(y, li[x])
    for _ in range(int(input())):
        x, y = map(int, readline().split())
        print(solve(x, y))
MAIN()
N, X, Y = map(int,input().split())
l = [0]*(N-1)
for i in range(1,N):
    for j in range(i+1,N+1):
        d=min(j-i,abs(X-i)+1+abs(Y-j),abs(Y-i)+1+abs(X-j))
        l[d-1] += 1
for a in l:
    print(a)


### FIXED CODE ###

S = input()
if S[2] != S[3] or S[4] != S[5]:
  print("No")
else:
  print("Yes")
from collections import Counter
n = int(input())
a = Counter(map(int, input().split()))
ans = n
for num, count in a.items():
    if count > 1:
        ans -= count
        continue
    if 1 in a and num != 1:
        ans -= count
        continue
    i = 2
    if num % i == 0:
            if i in a or (num // i) in a:
                ans -= count
                break
        i += 1
    while i * i <= num:
        if num % i == 0:
            if i in a or (num // i) in a:
                ans -= count
                break
        i += 2
print(ans)


### FIXED CODE ###

from collections import Counter
n = int(input())
a = Counter(map(int, input().split()))
MAX = 1000000 + 9
dp = [True] * MAX
for num in a:
    for i in range(num*2, MAX, num):
        dp[i] = False
ans = 0
for num in a:
    if dp[num] and a[num] == 1:
        ans += 1
print(ans)
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from math import floor

a, b, x = map(int, readline().split())
memo = floor(x / a) - 1
if memo >= 10 ** 9:
    print(10 ** 9)
    exit()
elif memo < 0:
    print(0)
    exit()
dg = len(str(memo))
for i in range(memo + 10 ** 5, 0, -1):
    if x - a * i >= b * dg:
        print(i)
        exit()
print(0)


### FIXED CODE ###

import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

n = int(readline())
ab = [list(map(int, readline().split())) for i in range(n - 1)]
gragh = [[] for j in range(n + 1)]
for a, b in ab:
    gragh[a].append(b)
    gragh[b].append(a)
root = 1
parent = [0] * (n + 1)
stack = [root]
order = []

while stack:
    x = stack.pop()
    order.append(x)
    for y in gragh[x]:
        if y == parent[x]:
            continue
        parent[y] = x
        stack.append(y)
color = [-1] * (n + 1)

for x in order:
    ng = color[x]
    cnt = 1
    for y in gragh[x]:
        if y == parent[x]:
            continue
        if cnt == ng:
            cnt += 1
        color[y] = cnt
        cnt += 1

ans = []
append = ans.append
for a, b in ab:
    if parent[a] == b:
        append(color[a])
    else:
        append(color[b])
print(max(ans))
print('\n'.join(map(str, ans)))
n=input()
x=[]
for i in range(n):
  w = input()
  if i !=0 and (w in x or x[i-1][-1] != w[0]):
    print ("No")
    exit()
  x.append(w)
print("Yes")
    

### FIXED CODE ###

n = int(input())
s  = input()
L = [s]
for i in range(n-1):
  t = input()
  if t[0] != s[len(s)-1] or t in L:
    print("No")
    exit()
  L.append(t)
  s = t
print("Yes")
N=int(input())
for n in range(N):
	sides = list(map(int,input().split()))
	sides.sort()
	if sides[-1] < sides[-2]+sides[-3]:
		print("YES")
	else:
		print("NO")

### FIXED CODE ###

N=int(input())
for n in range(N):
	sides = list(map(int,input().split()))
	sides.sort()
	if sides[-1]**2 == sides[-2]**2 + sides[-3]**2:
		print("YES")
	else:
		print("NO")
#!/usr/bin/env python3
import sys
import numpy as np
import math

def solve(A: int, B: int):
    print(A*B//math.gcd(A,B))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    solve(A, B)

if __name__ == '__main__':
    main()


### FIXED CODE ###

#!/usr/bin/env python3
import sys
import numpy as np
import fractions

def solve(A: int, B: int):
    print(A*B//fractions.gcd(A,B))
    return


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    A = int(next(tokens))  # type: int
    B = int(next(tokens))  # type: int
    solve(A, B)

if __name__ == '__main__':
    main()
x =input(input())
print(x*x*x)

### FIXED CODE ###

x = int(input())
print( x ** 3)
x = int(input())

if x < 1200:
    a = ABC
else:
    a = ARC

print(a)

### FIXED CODE ###

x = int(input())
 
if x < 1200:
    a = 'ABC'
else:
    a = 'ARC'
 
print(a)
X=int(input())
i=2
L=[]
import math
while math.floor(X**(1/i))!=1:
    L.append(math.floor(X**(1/i))**2)
    i+=1
print(max(L))

### FIXED CODE ###

N=int(input())
S=input()
L=[]
for i in range(0,N):
  L1=list(S[0:i]);L2=list(S[i:N])
  L.append(len(list(set(L1)&set(L2))))
print(max(L))
n, q = map(int, input().split())

class Node:
    def __init__(self, root):
        self.root = root
        self.children = []
        self.val = 0

    def add_child(self, child_node):
        self.children.append(child_node)
        if self.root is not None:
            self.root.add_child(child_node)

nodes = [None] * n
nodes[0] = Node(None)

for i in range(1, n):
    a, b = map(int, input().split())
    a_node = nodes[a - 1]
    if b - 1 not in nodes:
        b_node = Node(a_node)
        nodes[b - 1] = b_node
    a_node.add_child(nodes[b - 1])

for j in range(q):
    p, x = map(int, input().split())
    node = nodes[p - 1]
    node.val += x
    children = node.children
    for child in children:
        child.val += x

res = []
for node in nodes:
    res.append(str(node.val))
print(" ".join(res))

### FIXED CODE ###

n, q = map(int, input().split())

children_map = [None] * n
vals = [0] * n

for i in range(1, n):
    a, b = map(int, input().split())
    children_map[b - 1] = a - 1

for j in range(q):
    p, x = map(int, input().split())
    vals[p - 1] += x

res = []
for i in range(n):
    parent = children_map[i]
    if parent is not None:
        vals[i] += vals[parent]
    res.append(str(vals[i]))

print(" ".join(res))
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**5 + 1
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N,M = LI()
    S = SI()
    N0 = 2 ** (N-1).bit_length()
    st = [INF] * (2*N0)

    def update(i,x):
        i += N0-1
        st[i] = x
        while i > 0:
            i = (i-1) // 2
            st[i] = min(st[i*2+1], st[i*2+2])

    def query(l,r):
        L = l+N0; R = r+N0
        ret = INF
        while L < R:
            if L % 2:
                ret = min(ret,st[L-1])
                L += 1
            if R % 2:
                R -= 1
                ret = min(ret,st[R-1])
            L //= 2; R //= 2
        return ret

    update(0,0)

    for i in range(1,N+1):
        if S[i] == '1': continue
        else:
            if i <= M:
                update(i,1)
            else:
                x = query(i-M,i)
                update(i,x+1)
    if st[N+N0-1] >= INF:
        print(-1)
    else:
        ans = []
        g = st[N+N0-1]
        f = 0
        s = 1
        d = 0
        last_i = 0
        for i in range(1,N+1):
            if st[i+N0-1] == s+1:
                ans.append(d)
                f = last_i
                s += 1
            if st[i+N0-1] == s:
                d = i - f
                last_i = i
        ans.append(N-f)
        print(*ans,sep=' ')


if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**5 + 1
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N,M = LI()
    S = SI()
    N0 = 2 ** N.bit_length()
    st = [INF] * (2*N0)

    def update(i,x):
        i += N0-1
        st[i] = x
        while i > 0:
            i = (i-1) // 2
            st[i] = min(st[i*2+1], st[i*2+2])

    def query(l,r):
        L = l+N0; R = r+N0
        ret = INF
        while L < R:
            if L % 2:
                ret = min(ret,st[L-1])
                L += 1
            if R % 2:
                R -= 1
                ret = min(ret,st[R-1])
            L //= 2; R //= 2
        return ret

    update(0,0)

    for i in range(1,N+1):
        if S[i] == '1': continue
        else:
            if i <= M:
                update(i,1)
            else:
                x = query(i-M,i)
                update(i,x+1)
    if st[N+N0-1] >= INF:
        print(-1)
    else:
        ans = []
        f = N
        last_i = N
        s = st[N+N0-1]
        d = 1
        cnt = 0
        for i in range(N-1,-1,-1):
            cnt += 1
            if st[i+N0-1] == s-1:
                d = f - i
                last_i = i
            if cnt == M:
                ans.append(d)
                f = last_i
                s -= 1
                cnt = f - i
        if f > 0:
            ans.append(f)
        print(*reversed(ans),sep=' ')

if __name__ == '__main__':
    main()
def main(a, b):
    if (a * b) % 2 == 0:
        print('Even')
    else:
        print('Odd')

a = int(input())
b = int(input())
main(a, b)


### FIXED CODE ###

a, b = map(int, input().split())
if a*b % 2 == 0:
    print("Even")
else:
    print("Odd")
# -*- coding: utf-8 -*-

list = [input() for i in range(3)]

S = list[0]
T = list[1]

s = len(S)
t = len(T)


if t == s + 1 and S == T[: -1]:
    print('Yes')
else:
    print('No')
    

### FIXED CODE ###

# -*- coding: utf-8 -*-

list = []
for _ in range(2):
    list.append(input())

S = list[0]
T = list[1]

s = len(S)
t = len(T)


if t == s + 1 and S == T[: -1]:
    print('Yes')
else:
    print('No')
import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n, k = li()

if k > (n-1) * (n-2):
    print(-1)

else:
    edges = []
    for i in range(1,n):
        edges.append((1, i+1))

    surp = (n-1)*(n-2) // 2 - k
    for frm in range(1, n):
        for to in range(frm+1, n):
            if surp == 0:
                break

            edges.append((frm+1, to+1))
            surp -= 1


print(len(edges))
for ei, ej in edges:
    print(ei, ej)

### FIXED CODE ###

import sys
stdin = sys.stdin

sys.setrecursionlimit(10 ** 7)

def li(): return map(int, stdin.readline().split())
def li_(): return map(lambda x: int(x) - 1, stdin.readline().split())
def lf(): return map(float, stdin.readline().split())
def ls(): return stdin.readline().split()
def ns(): return stdin.readline().rstrip()
def lc(): return list(ns())
def ni(): return int(stdin.readline())
def nf(): return float(stdin.readline())

n, k = li()

if k > (n-1) * (n-2) // 2:
    print(-1)

else:
    edges = []
    for i in range(1,n):
        edges.append((1, i+1))

    surp = (n-1)*(n-2) // 2 - k
    for frm in range(1, n):
        for to in range(frm+1, n):
            if surp == 0:
                break

            edges.append((frm+1, to+1))
            surp -= 1


    print(len(edges))
    for ei, ej in edges:
        print(ei, ej)
def prod(array):
    res = 1
    for a, _ in array:
        res *= a
        res %= MOD
    return res

MOD = 10**9 + 7
N, K = map(int, input().split())
if N==K:
    res = 1
    for a in map(int, input().split()):
        res *= a
        res %= MOD
    print(res)
    exit()
A = []
allsgn = 0
for a in map(int, input().split()):
    A.append((abs(a), a < 0))
    allsgn += a<0
A.sort(reverse=True)
sgn = 0
s0 = -1
lastA = -1
for i, p in enumerate(A[:K]):
    a, s = p
    sgn += s
    if s != s0:
        lastA = i-1
    s0 = s
lasts = s0
if allsgn==N:
    if K%2==1:
        print((-prod(A[-K:]))%MOD)
        exit()
    else:
        print(prod(A[:K])%MOD)
        exit()
if sgn%2==0:
    print(prod(A[:K])%MOD)
    exit()
if lasts != A[K][1]:
    res = prod(A[:K-1])*A[K][0]
else:
    firstA = None
    for i, p in enumerate(A[K:],start=K):
        a, s = p
        if s != lasts:
            firstA = i
            break
    if (firstA is not None and A[K][0]/A[lastA][0] < A[firstA][0]/A[K-1][0]) or lastA == -1:
        res = prod(A[:K - 1]) * A[firstA][0]
    else:
        res = prod(A[:lastA]) * prod(A[lastA + 1:K + 1])
print(res%MOD)
"""
6 3
5 -3 -3 -3 -3 -2

10 7
-10 -10 -10 -10 10 10 10 10 10 10
"""

### FIXED CODE ###

def prod(array):
    res = 1
    for a, _ in array:
        res *= a
        res %= MOD
    return res

MOD = 10**9 + 7
N, K = map(int, input().split())
if N==K:
    res = 1
    for a in map(int, input().split()):
        res *= a
        res %= MOD
    print(res)
    exit()
A = []
allsgn = 0
for a in map(int, input().split()):
    A.append((abs(a), a < 0))
    allsgn += a<0
A.sort(reverse=True)
sgn = 0
s0 = -1
lastA = -1
for i, p in enumerate(A[:K]):
    a, s = p
    sgn += s
    if s != s0:
        lastA = i-1
    s0 = s
lasts = s0
if allsgn==N:
    if K%2==1:
        print((-prod(A[-K:]))%MOD)
        exit()
    else:
        print(prod(A[:K])%MOD)
        exit()
if sgn%2==0:
    print(prod(A[:K])%MOD)
    exit()
if lasts != A[K][1]:
    res = prod(A[:K-1])*A[K][0]
else:
    firstA = None
    for i, p in enumerate(A[K:],start=K):
        a, s = p
        if s != lasts:
            firstA = i
            break
    if lastA == -1 or (firstA is not None and A[K][0]/A[lastA][0] < A[firstA][0]/A[K-1][0]):
        res = prod(A[:K - 1]) * A[firstA][0]
    else:
        res = prod(A[:lastA]) * prod(A[lastA + 1:K + 1])
print(res%MOD)
"""
6 3
5 -3 -3 -3 -3 -2

10 7
-10 -10 -10 -10 10 10 10 10 10 10
"""
import math
N = int(input())
capacity = min([int(input()) for i in range(6)])

print(math.ceil(N/capacity)+4)

### FIXED CODE ###

import math
N = int(input())
capacity = min([int(input()) for i in range(5)])

print(math.ceil(N/capacity)+4)
def k(x):
  x=int(x)
  if x%2==0 and (x%3==0 or x%5==0):
	return 1
  return 1
n=int(input());l=list(map(k,input().split()))
if sum(l)==n:
  print("APPROVED")
else:
  print("DENIED")

### FIXED CODE ###

def k(x):
  x=int(x)
  if x%2==0:
    if (x%3==0 or x%5==0):
      return 1
    return 0
  return 1
n=int(input());l=list(map(k,input().split()))
if sum(l)==n:
  print("APPROVED")
else:
  print("DENIED")
import sys 
RESOURC_DICT = dict([(k,v) for k,v in zip(range(1,10),[2,5,5,4,5,6,3,7,6])])

def get_input():
    N,M = [ int(_) for _ in input().split()]
    A = [int(_) for _ in input().split()]
    return N,M,A


def solve(N,M,A):
    dp = [-1 for _ in range(N+1)]
    for i in range(N+1):
        fill(A,dp,i)
    return dp[N]


def count_digit(number,current=0):
    if number == 0:
        return current
    else:
        return count_digit(number//10,current=current+1)

def fill(A,dp,n):
    cand = []
    for number in A:
        weight = RESOURC_DICT[number]
        if n -weight>=0:
            if n- weight == 0:
                cand.append(number)
            else:
                if dp[n-weight] != -1:
                    cand.append(max(
                        10**count_digit(dp[n-weight])*number + dp[n-weight],
                        10 * dp[n-weight] +number  )
                        )
                else:
                    cand.append(-1)
        else:
            cand.append(-1)
    dp[n] = max(cand)


if __name__ =="__main__":
    N,M,A = get_input()
    solve(N,M,A)
    print(ans)

### FIXED CODE ###

import sys 
RESOURCE_DICT = dict([(k,v) for k,v in zip(range(1,10),[2,5,5,4,5,6,3,7,6])])

def get_input():
    N,M = [ int(_) for _ in input().split()]
    A = [int(_) for _ in input().split()]
    return N,M,A


def solve(N,M,A):
    sorted_A = sorted(A)[::-1]
    dp = fill_dp(N,sorted_A)
    array = find_array(dp,N,sorted_A)
    return "".join(map(str,array))

def fill_dp(N,A):
    dp = [ -100*N for _ in range(N+1)]
    dp[0] = 0
    for i in range(1,N+1):
        fill(A,dp,i,N)
    return dp

def fill(A,dp,n,N):
    cand = [ dp[n-RESOURCE_DICT[num]]+1 for num in A if n - RESOURCE_DICT[num] >= 0 ]
    dp[n] = max(cand) if len(cand) > 0  else -100*N

def find_array(dp,N,sorted_A):
    array = []
    current_n = N
    for _ in range(dp[N]):
        for number in sorted_A:
            if current_n-RESOURCE_DICT[number] >= 0  and dp[current_n-RESOURCE_DICT[number]] == dp[current_n]-1:
                array.append(number)
                current_n -= RESOURCE_DICT[number]
                break
    return array


if __name__ =="__main__":
    N,M,A = get_input()
    ans = solve(N,M,A)
    print(ans)
# 入力
N, X, T = input().split()

if N % X == 0:
    cnt = N / X
else:
    cnt = int(N / X + 1)

print(cnt * T)

### FIXED CODE ###

# 入力
N, X, T = input().split()

N = int(N)
X = int(X)
T = int(T)

if N % X == 0:
    cnt = int(N / X)
else:
    cnt = int(N / X + 1)

print(cnt * T)
n = int(input())
edges = [[] for _  in range(n)]

for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)

height = [None for _ in range(n)]
root = 0
height[root] = 0

def build(node):
    for to in edges[node]:
        if height[to] == None:
            height[to] = height[node] + 1
            build(to)

build(root)

enum = [(-h, i) for i, h in enumerate(height)]
enum.sort()

score = 0
tree = [None] * n
clst = sorted(map(int, input().split()))

for c, hi in zip(clst, enum):
    h, i = hi
    tree[i] = c

print(sum(clst[:-1]))
print(*tree)


### FIXED CODE ###

import sys
sys.setrecursionlimit(100000)

n = int(input())
edges = [[] for _  in range(n)]

for _ in range(n - 1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    edges[a].append(b)
    edges[b].append(a)

height = [None for _ in range(n)]
root = 0
height[root] = 0

def build(node):
    for to in edges[node]:
        if height[to] == None:
            height[to] = height[node] + 1
            build(to)

build(root)

enum = [(-h, i) for i, h in enumerate(height)]
enum.sort()

tree = [None] * n
clst = sorted(map(int, input().split()))

for c, hi in zip(clst, enum):
    h, i = hi
    tree[i] = c

print(sum(clst[:-1]))
print(*tree)
import sys
import math
import bisect
stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N,M=nm()
Bl=[[] for i in range(10**5)]

for i in range(N):
    A,B=nm()
    Bl[A].append(B)

now=Bl[0]
ans=0
for i in range(1,M+2):
    if(now!=[]):
        max_val=max(now)
        ans+=max_val
        now.remove(max_val)
    now=now+Bl[i]

print(ans)


### FIXED CODE ###

import sys
import math
import bisect
import heapq
stdin = sys.stdin

ns = lambda: stdin.readline().rstrip()
ni = lambda: int(stdin.readline().rstrip())
nm = lambda: map(int, stdin.readline().split())
nl = lambda: list(map(int, stdin.readline().split()))

N,M=nm()
Bl=[[] for i in range(10**5+5)]

for i in range(N):
    A,B=nm()
    Bl[A].append(-B)

now=[]
if(Bl[0]!=[]):
    for i in range(len(Bl[0])):
        heapq.heappush(now,Bl[0][i])

ans=0
for i in range(1,M+2):
    if(now!=[]):
        max_val=heapq.heappop(now)
        ans+=max_val
    if(Bl[i]!=[]):
        for j in range(len(Bl[i])):
            heapq.heappush(now,Bl[i][j])

print(-ans)
def dfs(n):
    visited[n-1] = True
    for node in graph[n-1]:
        if visited[node-1]:
            continue
        point[node-1] += point[n-1]
        dfs(node)


n, q = LI()
point = [0]*n
graph = [[] for i in range(n)]

for i in range(n - 1):
    a, b = LI()
    graph[a - 1].append(b)
    graph[b - 1].append(a)


for i in range(q):
    p, x = LI()
    point[p - 1] += x

visited = [False]*n

dfs(1)

print(*point)


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
 
N, Q = map(int, input().split())
root = [[] for _ in range(N)]
ans = [0]*N
 
for _ in range(N-1):
  a,b=map(int, input().split())
  a,b=a-1,b-1
  root[a].append(b)
  root[b].append(a)

for _ in range(Q):
  p,x=map(int, input().split())
  ans[p-1]+=x
  
visited=[False]*N
def dfs(v):
  visited[v] = True
  for go in root[v]:
    if visited[go]:
      continue
    ans[go] += ans[v]
    dfs(go)

dfs(0)
print(*ans)
from itertools import groupby

s = input()
k = int(input())
a, b = [], []
count1, count2 = 0, 0

for key, group in groupby(s):
  a.append(list(group))

if len(a) == 1:
  print(len(s*k) // 2)
  
else:
  for key, group in groupby(s*2):
    b.append(list(group))  

  for i in a:
    count1 += len(i) // 2

  for i in b:
    count2 += len(i) // 2

  c = count2 - count1

  print(count1 + c*(k-1))

### FIXED CODE ###

from itertools import groupby

s = input()
k = int(input())
a, b = [], []
count1, count2 = 0, 0

for key, group in groupby(s):
  a.append(list(group))

if len(a) == 1:
  if len(s)%2==1 and k%2==1:
    print(len(s)//2 + len(s)*(k-1)//2 )
  else:
    print(len(s)*k//2)
else:
  for key, group in groupby(s*2):
    b.append(list(group))  

  for i in a:
    count1 += len(i) // 2

  for i in b:
    count2 += len(i) // 2

  c = count2 - count1

  print(count1 + c*(k-1))
x, y = raw_input().split(' ')
print '{} {}'.format(x*y, (x+y)*2)

### FIXED CODE ###

#!/usr/bin/env python

if __name__=='__main__':
    a, b = map(int, raw_input().split())
    print "{} {}". format(a*b, 2*(a+b))
N, M = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(M):
  x, y = map(lambda x: int(x)-1, input().split())
  graph[x].append(y)

dp = [-1]*N
def rec(v):
  if dp[v] != -1:
    return dp[v]
  
  res = 0
  for nv in graph[v]:
    res = max(res, rec(nv)+1)
  dp[v] = res
  return res

res = 0
for i in range(N):
  res = max(res, rec(i))
print(res)

### FIXED CODE ###

from sys import setrecursionlimit
setrecursionlimit(10**7)

N, M = map(int, input().split())
graph = [[] for _ in range(N)]
for _ in range(M):
  x, y = map(lambda x: int(x)-1, input().split())
  graph[x].append(y)

dp = [-1]*N
def rec(v):
  if dp[v] != -1:
    return dp[v]
  
  res = 0
  for nv in graph[v]:
    res = max(res, rec(nv)+1)
  dp[v] = res
  return res

res = 0
for v in range(N):
  res = max(res, rec(v))
print(res)