Harryxun/stf_run huggingface dataset:
from collections import deque

H,W=map(int,input().split())
if H==1 and W==1:
    print(0)
    exit()

A=[]
B=deque([])
C=[[-1]*W for i in range(H)]

for h in range(H):
    temp=list(input())
    A.append(temp)
    for w in range(W):
        if A[h][w]=="#":
            B.append([h,w,0])
            C[h][w]=0



step=[[1,0],[-1,0],[0,1],[0,-1]]
ans=[]
while B:
    P=B.popleft()
    for x,y in step:
        if 0<=P[0]+y<=H-1 and 0<=P[1]+x<=W-1:
            if C[P[0]+y][P[1]+x]==-1:
                B.append([P[0]+y,P[1]+x,P[2]+1])
                C[P[0]+y][P[1]+x]=1
                ans.append(P[2]+1)


print(max(ans))




### FIXED CODE ###

from collections import deque

H,W=map(int,input().split())
if H==1 and W==1:
    print(0)
    exit()

A=[]
B=deque([])
C=[[-1]*W for i in range(H)]
ans=[]
for h in range(H):
    temp=list(input())
    A.append(temp)
    for w in range(W):
        if A[h][w]=="#":
            B.append([h,w,0])
            C[h][w]=0
            ans.append(0)



step=[[1,0],[-1,0],[0,1],[0,-1]]

while B:
    P=B.popleft()
    for x,y in step:
        if 0<=P[0]+y<=H-1 and 0<=P[1]+x<=W-1:
            if C[P[0]+y][P[1]+x]==-1:
                B.append([P[0]+y,P[1]+x,P[2]+1])
                C[P[0]+y][P[1]+x]=1
                ans.append(P[2]+1)


print(max(ans))
n,x=input().split()
L = list(input().split())
d = 0
i = 0
while d <= int(x):
    d+=int(L[i])
    i+=1
print(i)

### FIXED CODE ###

n, x = map(int, input().split())
result, cur = 1, 0

for jump in map(int, input().split()):
    
    cur += jump
    
    if cur <= x:
        result += 1
    else:
        break

print(result)
n = int(input())
l = []
t = 1
for _ in range(n):
  x = list(map(int,input().split())
  l.append(x)
count = 0
for i, j in l:
  if i == j:
    count += 1
  if i!=j:
    count = 0
  if count == 3:
  	t = 0
    print("Yes")
    break
if t:
  print("No")
  

### FIXED CODE ###

n = int(input())
count = 0
t = 1
for _ in range(n):
  a, b = map(int,input().split())
  if a==b:
    count += 1
  if a!=b:
    count = 0
  if count == 3:
    t = 0
if t:
  print("No")
else:
  print("Yes")
s=input()
n=len(s)
sum=0

for i in range(n):
  if s(i)!=s(n-i-1):
    sum+=1
    print(sum//2)


### FIXED CODE ###

s=input()
cnt=0
for i in range(len(s)//2):
  if s[i]!=s[(i*(-1))-1]:
    cnt+=1
print(cnt)
S = input()

x = len(S)-1

a = 0
b = 0
s = 0
for i in range(1 << x):
    for j in range(x):
        if ((i >> j) & 1 == 1):
            b = j
            s += int(S[a:b])
            a = j

print(s)

### FIXED CODE ###

S = input()

x = len(S)-1

s = 0
for i in range(1 << x):
    t = 0
    for j in range(x):
        if ((i >> j) & 1 == 1):
            s += int(''.join(S[t:j+1]))
            t = j+1
    s += int(''.join(S[t:]))
print(s)
N,A,B=map(int,input().split())
ball=('b'*A+'a'*B)*10**18
print(ball.count('b',0,N))


### FIXED CODE ###

N,A,B=map(int,input().split())
if N == (N//(A+B))*(A+B):
    print(N-(N//(A+B))*(B))
elif (N//(A+B))*(A+B)<N <= (N//(A+B))*(A+B)+A:
    print(N-(N//(A+B))*(B))
else:
    print((N//(A+B)+1)*(A))
A, B, C, D = map(int,input().split())
ans = "Yes"
curA = A
curC = C
for i in range(max(A,C)):
  curC  = curC-B
  if(curC<=0):
    ans = "Yes"
    break
  curA = curA-D
  if(curA<=0):
    ans = "No"
    break
print(ans)

### FIXED CODE ###

S, W = map(int,input().split())
ans = "safe"
if(S<=W):
  ans = "unsafe"
print(ans)
import math

A,B = [int(x) for x in input().split()]
print(math.ceil(B/A))

### FIXED CODE ###

S = input()
T = input()
count = 0

for n in range(3):
    if S[n] == T[n]:
        count += 1

print(count)
import math
x = input()
x = int(x)
if x in [2,3,5,7]:
    print(x)
elif x == 4:
    print(5)
elif x==6:
    print(7)
elif x==8:
    print(11)
else:
    while:
        flag = 0
        if x%2==0:
            x= x+1
            continue
        y = 3
        while y<math.sqrt(x):
            if x%y==0:
                break
            y=y + 2
            if y>math.sqrt(x):
                flag=1
                break
        if flag == 1:
            print(x)
            break
        x = x+2


### FIXED CODE ###

import math
x = input()
x = int(x)
if x in [2,3,5,7]:
    print(x)
elif x == 4:
    print(5)
elif x==6:
    print(7)
elif x==8:
    print(11)
else:
    while 1:
        flag = 0
        if x%2==0:
            x= x+1
            continue
        y = 3
        while y<math.sqrt(x):
            if x%y==0:
                break
            y=y+2
            if y>math.sqrt(x):
                flag=1
                break
        if flag == 1:
            print(x)
            break
        x = x+2
n = int(input())
s = list(input())
r = []
g = []
b = []
for i,j in enumerate(s):
    if j == "R":
        r.append(i)
    elif j == "G":
        g.append(i)
    else:
        b.append(i)
ans = 0
ss = [len(r),len(g),len(b)]
if max(ss) == len(b):
    for i in r:
        for j in g:
            sa = abs(i-j)
            if sa%2==0:
                p = (i+j)//2
                if p in b:
                    ans-=1
                    ans += len(b)
            if i<j:
                if (i-sa) in b:
                    ans -= 1
                if (j+sa) in b:
                    ans -= 1
            if i>j:
                if (j-sa) in b:
                    ans -= 1
                if (i+sa) in b:
                    ans -= 1
if max(ss) == len(g):
    for i in r:
        for j in b:
            sa = abs(i-j)
            if sa%2==0:
                p = (i+j)//2
                if p in g:
                    ans-=1
                    ans += len(g)
            if i<j:
                if (i-sa) in g:
                    ans -= 1
                if (j+sa) in g:
                    ans -= 1
            if i>j:
                if (j-sa) in g:
                    ans -= 1
                if (i+sa) in g:
                    ans -= 1
if max(ss) == len(r):
    for i in b:
        for j in g:
            sa = abs(i-j)
            if sa%2==0:
                p = (i+j)//2
                if p in r:
                    ans-=1
                    ans += len(r)
            if i<j:
                if (i-sa) in r:
                    ans -= 1
                if (j+sa) in r:
                    ans -= 1
            if i>j:
                if (j-sa) in r:
                    ans -= 1
                if (i+sa) in r:
                    ans -= 1

print(ans)


### FIXED CODE ###

s = list(input())
if "7" in s:
    print("Yes")
else:
    print("No")
import numpy

n, d = map(int, input().split())
x = [list(map(int, input().split())) for i in range(n)]
cnt=0
for i in range(n-1):
    for j in range(i + 1, n):
        a = numpy.array(x[i])
        b = numpy.array(x[j])
        num=numpy.linalg.norm(a-b)
        if num.is_integer() == True:
            cnt += 1
print(cnt)

### FIXED CODE ###

import numpy

n, d = map(int, input().split())
x = [list(map(int, input().split())) for i in range(n)]
cnt=0
for i in range(n-1):
    for j in range(i + 1, n):
        a = numpy.array(x[i])
        b = numpy.array(x[j])
        num=numpy.linalg.norm(a-b)
        if num.is_integer() == True:
            cnt += 1
print(cnt)
# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n,a,b = MAP()

q = n//(a+b)
rem = n%(a+b)

ans = q*a
if(rem>a):
    print(ans + a)
else:
    print(ans + rem)

### FIXED CODE ###

# Template 1.0
import sys, re
from collections import deque, defaultdict, Counter, OrderedDict
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from heapq import heappush, heappop, heapify, nlargest, nsmallest
def STR(): return list(input())
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def list2d(a, b, c): return [[c] * b for i in range(a)]
def sortListWithIndex(listOfTuples, idx):   return (sorted(listOfTuples, key=lambda x: x[idx]))
def sortDictWithVal(passedDic):
    temp = sorted(passedDic.items(), key=lambda kv: (kv[1], kv[0]))
    toret = {}
    for tup in temp:
        toret[tup[0]] = tup[1]
    return toret
def sortDictWithKey(passedDic):
    return dict(OrderedDict(sorted(passedDic.items())))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

n,a,b = MAP()

q = n//(a+b)
rem = n%(a+b)

ans = q*a
if(rem>a):
    print(ans + a)
else:
    print(ans + rem)
N, M = map(int, input().split())
A = list(map(int, input().split()))
sum = 0

for i in A:
    sum += i

if sum > N:
    print(-1)
else:
    print(n - sum)    

### FIXED CODE ###

n,m = map(int,input().split())
A = list(map(int, input().split()))
 
a = sum(A)
 
ans = n-a
if ans < 0:
    ans = -1
 
print(ans)
import heapq

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

a = [[] for i in range(m)]
d = []
heapq.heapify(d)

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

#print(a)

ans = 0
for i in range(m):
    for j in range(len(a[i])):
        heapq.heappush(d,a[i][j])
    ans -= heapq.heappop(d)

print(ans)

### FIXED CODE ###

import heapq

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

a = [[] for i in range(100000)]
d = []
heapq.heapify(d)

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

#print(a)

ans = 0
for i in range(m):
    for j in range(len(a[i])):
        heapq.heappush(d,a[i][j])
    if d != []:
        ans -= heapq.heappop(d)

print(ans)
n = int(ipnut())
cnt = [0] * 10001
for x in range(1, 101):
    for y in range(1, 101):
        for z in range(1, 101):
            v = x*x + y*y + z*z + x*y + y*z + z*x
            if v <= n:
                cnt[v] += 1

for i in range(1, n + 1):
    print(cnt[i])


### FIXED CODE ###

import math
import sys


def input(): return sys.stdin.readline().strip()
def I(): return int(input())
def LI(): return list(map(int, input().split()))
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def S(): return input()
def LS(): return input().split()


INF = float('inf')


def f(m):
    res = 0
    r_m = math.sqrt(m)
    for i in range(1, int(r_m) + 1):
        for j in range(1, int(r_m) + 1):
            for k in range(1, int(r_m) + 1):
                if i ** 2 + j ** 2 + k ** 2 + i * j + j * k + k * i == m:
                    print(f'{(m, i, j, k)=}')
                    res += 1
    return res


n = I()
cnt = [0] * 10001
r_n = int(math.sqrt(n)) + 1
for x in range(1, r_n):
    for y in range(1, r_n):
        for z in range(1, r_n):
            v = x**2 + y**2 + z**2 + x*y + y*z + z*x
            if v <= n:
                cnt[v] += 1

for i in range(1, n + 1):
    print(cnt[i])
#C - Streamline(ABC117)
N,M=map(int, input().split())
X=list(map(int, input().split()))
X.sort(reverse=True)
ans=X[0]-X[-1]
A=[0]*(N-1)
if N<M:
    for i in range(M-1):
        a=X[i]-X[i+1]
        if min(A)<a:
            A[A.index(min(A))]=a
    ans-=sum(A)
else:
    ans=0
print(ans)

### FIXED CODE ###

#C - Streamline(ABC117)
N,M=map(int, input().split())
X=list(map(int, input().split()))
X.sort(reverse=True)
ans=X[0]-X[-1]
A=[]
if N<M:
    for i in range(M-1):
        A.append(X[i]-X[i+1])
    A.sort(reverse=True)
    ans-=sum(A[0:N-1])
else:
    ans=0
print(ans)
n=int(input())
x=str(input())
for i in range(n):
    if x[i]=='0':
        tmp=x[:i]+'1'+x[(i+1):]
    elif x[i]=='1':
        tmp=x[:i]+'0'+x[(i+1):]
    cnt=0
    while True:
        r=int(tmp,2)%tmp.count('1')
        tmp='{:b}'.format(r)
        cnt+=1
        if tmp=='0':
            print(cnt)
            break

### FIXED CODE ###

n=int(input())
X=input()
x=X.count('1')
x_plus=int(X,2)%(x+1)
keta=n-1
if x==1:
    for i in range(n):
        tmp=0
        ans=1
        if X[i]=='0':
            tmp+=x_plus
            tmp+=pow(2,keta,(x+1))
            tmp%=(x+1)
            while tmp:
                tmp=tmp%(bin(tmp).count('1'))
                ans+=1
            print(ans)
        else:
            print(0)
        keta-=1
else:
    x_minus=int(X,2)%(x-1)
    for i in range(n):
        tmp=0
        ans=1
        if X[i]=='0':
            tmp+=x_plus
            tmp+=pow(2,keta,(x+1))
            tmp%=(x+1)
            while tmp:
                tmp=tmp%(bin(tmp).count('1'))
                ans+=1
            print(ans)
        else:
            tmp+=x_minus
            tmp-=pow(2,keta,(x-1))
            tmp%=(x-1)
            while tmp:
                tmp=tmp%(bin(tmp).count('1'))
                ans+=1
            print(ans)
        keta-=1
n = int(input())
A = sorted(list(map(int,input().split())))
A_card = []
B_card = []
for i in range(n):
        if i%2 == 0:
                A_card.append(A[i])
        else:
                B_card.append(A[i])
print(sum(A_card) - sum(B_card))

### FIXED CODE ###

n = int(input()) % 500
a = int(input())
if a >= n:
    print('Yes')
else:
    print('No')
# 1 <= N <= 10 ** 5
# 0 <= M <= N − 1
# 1 <= a <= N - 1
N, M = map(int, input().split())

memo = {1: 1, 0: 1}
def fibo(n):
  if n in memo:
    return memo[n]
  else:
    num = fibo(n - 1) + fibo(n - 2)
    memo[n] = num
    return num


stairs = [False for _ in range(N + 1)]
pattern = 1
prev = 0
for _ in range(M):
  a = int(input())
  sub = a - prev - 1
  if sub == -1:
    print(0)
    break
  pattern *= fibo(sub)
  prev = a + 1
else:
  sub = N - prev
  pattern *= fibo(sub)

  print(pattern % 1000000007)



### FIXED CODE ###

# 1 <= N <= 10 ** 5
# 0 <= M <= N − 1
# 1 <= a <= N - 1
N, M = map(int, input().split())

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


stairs = [False for _ in range(N + 1)]
pattern = 1
prev = 0
for _ in range(M):
  a = int(input())
  sub = a - prev - 1
  if sub == -1:
    print(0)
    break
  pattern *= memo[sub]
  prev = a + 1
else:
  sub = N - prev
  pattern *= memo[sub]
  print(pattern % 1000000007)
n, m = [int(i) for i in input().split()]
d = sorted({i[0]: i[1] for i in [[int(j) for j in input().split()] for k in range(n)]}.items(), key = lambda x:x[0])
sm = 0
i = 0
while True:
    if m > d[i][1]:
        sm += d[i][0] * d[i][1]
        m -= d[i][1]
    else:
        sm += d[i][0] * m
        break
    i += 1
print(sm)

### FIXED CODE ###

n, m = [int(i) for i in input().split()]
ab = [[int(i) for i in input().split()] for j in range(n)]
ab = sorted(ab, key=lambda x:x[0])

sm = 0
i = 0
for i in ab:
    if m > i[1]:
        sm += i[0] * i[1]
        m -= i[1]
    else:
        sm += i[0] * m
        break
print(sm)
for h in range(1, 10000):
    w = int(raw_input())
    if w == 0:
        break
    n = int(raw_input())
    a = [map(int, raw_input().split(',')) for i in range(n)]
    d = [[None] * 1010 for i in range(1010)]

    def f(ix, w):
        if ix == n:
            return 0, 0
        if d[ix][w] is not None:
            return d[ix][w]
        mx = f(ix + 1, w)
        if w >= a[ix][1]:
            e = [p + q for p, q in zip(f(ix + 1, w - a[ix][1]), a[ix])]
            if mx[0] < e[0] or (mx[0] == e[0] and mx[1] > e[1]):
                mx = e
        d[ix][w] = mx
        return mx
    mx = f(0, w)
    print 'Case {}:'.format(h)
    print mx[0]
    print mx[1]

### FIXED CODE ###

from itertools import *

def mlist(n, *args, **keys):
    if args:
        return [mlist(*args, **keys) for i in range(n)]
    else:
        return [keys.get('default')] * n

for h in count(1):
    w = int(raw_input())
    if w == 0: break
    n = int(raw_input())
    a, b = zip(*[map(int, raw_input().split(',')) for i in range(n)])
    c = mlist(1010, 1010)
    c[0][0] = 0
    for i in range(n):
        for j in range(w + 1):
            if c[i][j] is None: continue
            c[i + 1][j] = max(c[i + 1][j], c[i][j])
            if j + b[i] <= w:
                c[i + 1][j + b[i]] = max(c[i + 1][j + b[i]], c[i][j] + a[i])
    mx = max(c[n])
    print 'Case {}:'.format(h)
    print mx
    print c[n].index(mx)
n,a,b=int(input().split())

print(min(n*a,b))

### FIXED CODE ###

n,a,b=map(int,input().split())
print(min(n*a,b))
from fractions import gcd
from functools import reduce
N = int(input())
a = list(map(int, input().split()))

list_ans = []

for i in range(N):
    remove_a = a.pop(0)
    #print("a is",a)
    ans = reduce(gcd, a)
    list_ans.append(ans)
    a.append(remove_a)
    #print("fix a is",a)

print(max(list_ans))

### FIXED CODE ###

n = int(input())

div_count = 0
sum = 0

for i in range(3,n+1,2):
    a = i
    j = 2
    div_count = 0
    while(j <= a):
        #print("a is",a)
        #print("j is",j)
        if a % j == 0:
            div_count += 1
            a //= j
            j += 1
        else:
            j += 1
    if div_count == 3:
        sum += 1

print(sum)
N,M=map(int,input().split())
a=[]
for i in range(0,M):
    a.append(int(input()))
if M==0:
    a=[-1]
ans=1
s=[-1]*(N+1)
s[0]=1
def dp(n):
    if n==0:
        return 1
    elif n==1:
        return 0 if 1 in a else 1
    if s[n-1]!=-1:
        c=s[n-1]
    elif n-1 in a:
        s[n-1]=0
        c=0
    else:
        c=dp(n-1)%1000000007
        s[n-1]=c
    if s[n-2]!=-1:
        b=s[n-2]
    elif n-2 in a:
        s[n-2]=0
        b=0
    else:
        b=dp(n-2)%1000000007
        s[n-2]=b
    return (c+b)%1000000007
print(dp(N)%1000000007)

### FIXED CODE ###

import sys #追加
sys.setrecursionlimit(50000000) #追加
N,M=map(int,input().split())
a=[]
for i in range(0,M):
    a.append(int(input()))
a=set(a)
if M==0:
    a=[-1]
ans=1
s=[-1]*(N+1)
s[0]=1
def dp(n):
    if n==0:
        return 1
    elif n==1:
        return 0 if 1 in a else 1
    if s[n-1]!=-1:
        c=s[n-1]
    elif n-1 in a:
        s[n-1]=0
        c=0
    else:
        c=dp(n-1)%1000000007
        s[n-1]=c
    if s[n-2]!=-1:
        b=s[n-2]
    elif n-2 in a:
        s[n-2]=0
        b=0
    else:
        b=dp(n-2)%1000000007
        s[n-2]=b
    return (c+b)%1000000007
print(dp(N)%1000000007)
n = int(input())
a = list(map(int,input().split()))
front = 0
rear = sum(a)
ans = 10**100
for i in range(n-1):
    front += a[i]
    rear -= a[i]
    ans = min(ans,abs(front - rear))
print(ans)

### FIXED CODE ###

n , d = map(int,input().split())
import math

ans = 0
cnt = 0
for i in range(n):
    a,b = map(int,input().split())
    if math.sqrt(a**2 + b**2) <= d:
        ans += 1
print(ans)
n = int(input())
#a,b = map(int,input().split())
li = list(map(int,input().split()))
li.sort()
#print(li)
num = 0

for i in range(n-4):
    if li[i] == li[i+1] ==li[i+2] == li[i+3]:
        li.pop(li[i])
for i in range(n):
    for j in range(i+1,n):
        for k in range(j+1,n):
            if li[i]+li[j]>li[k] and li[i]<li[j]+li[k] and li[i]+li[k]>li[j]:
                num += 1
print(num)

### FIXED CODE ###

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

num = 0
import bisect
for i in range(n):
    for j in range(i+1,n):
        num += bisect.bisect_left(li,li[i]+li[j])-(j+1)
print(num)
import sys

a = []
for line in sys.stdin:
    a.append(int(line))
    
del a[0]
del a[len(a)-1]
b = []
for line in sys.stdin:
    b.append(int(line))

del b[0]
del b[0]
print(max(b)-min(a))
    



### FIXED CODE ###

import sys

a = []
b = []
for line in sys.stdin:
    a.append(int(line))
    b.append(int(line))

del a[0]
del a[len(a)-1]
del b[0]
del b[0]

maxv = -1000000000
minv = a[0]
for i in range(len(a)):
    maxv = max(maxv,b[i]-minv)
    minv = min(minv,b[i])
print(maxv)
a = []
for i in (5):
  a += [int(input())]
k = int(input())
a.sort()
kekka = ":("
if a[4] - a[0] <= k:
  kekka = "Yay!"
print(kekka)

### FIXED CODE ###

a = []
for i in range (5):
  a += [int(input())]
k = int(input())
a.sort()
kekka = ":("
if a[4] - a[0] <= k:
  kekka = "Yay!"
print(kekka)
#                         author:  kagemeka 
#                         created: 2019-11-07 02:35:07(JST)
import sys
# import collections
# import math
# import string
# import bisect
# import re
# import itertools
# import statistics
# import functools
# import operator

def main():
    a, b, k = (int(i) for i in sys.stdin.readline().split())
    ab = list(range(a, b+1))
    if k * 2 >= len(ab):
        res = ab
    else:
        res = ab[:k] + ab[-k:]
    for r in res:
        print(r)
    
if __name__ == "__main__":
    # execute only if run as a script
    main()

# 何故

### FIXED CODE ###

import sys

a, b, k = map(int, sys.stdin.readline().split())

def main():
    if k >= b - a + 1:
        res = list(range(a, b+1))
    else:
        res = set(range(a, a+k)) | set(range(b, b-k, -1))
        res = sorted(res)
    return res

if __name__ == '__main__':
    ans = main()
    print(*ans, sep='\n')
class Dice:
    def __init__(self,t,f,r,l,b,u):
        self.a=[[t,f,r,l,b,u]]
        self.direction={'S':(4,0,2,3,5,1),'N':(1,5,2,3,0,4),'E':(3,1,0,5,4,2),'W':(2,1,5,0,4,3),'Y':(0,3,1,4,2,5)}
    def roll(self,d,n):
        self.a[n]=[self.a[n][i] for i in self.direction[d]]
        self.t = self.a[0]
        self.f = self.a[1]
        self.r = self.a[2]
        self.l = self.a[3]
        self.b = self.a[4]
        self.u = self.a[5]
n=int(input())
for i in range(n):
    if i==0:
        t,f,r,l,b,u = map(int, input().split())
        dice = Dice(t, f, r, l, b, u)
    else:
        a = list(map(int, input().split()))
        dice.a.append(a)
s='SSSWEEE'
w='YYYY'
ans='Yes'
for u in range(n-1):
    for t in range(u+1,n):
        for j in s:
            for r in w:
                if dice.a[t]==dice.a[u]:
                    ans='No'
                    break
                dice.roll(r,t)
            else:
                dice.roll(j,t)
                continue
            break
        else:
            continue
        break
    else:
        continue
    break
print(ans)



### FIXED CODE ###

class Dice:
    def __init__(self,t,f,r,l,b,u):
        self.a=[[t,f,r,l,b,u]]
        self.direction={'S':(4,0,2,3,5,1),'N':(1,5,2,3,0,4),'E':(3,1,0,5,4,2),'W':(2,1,5,0,4,3),'U':(5,4,2,3,1,0)}
    def dice_tuple(self):
        self.b=tuple(self.a)
    def roll(self,d,n):
        self.a[n]=[self.a[n][i] for i in self.direction[d]]
    def roll2(self,d,n):
        self.a[n]=[self.b[n][i] for i in self.direction[d]]
    def roll_w(self, n):
        self.a[n] = [self.a[n][i] for i in (0, 3, 1, 4, 2, 5)]
n=int(input())
for i in range(n):
    if i==0:
        t, f, r, l, b, u = map(int, input().split())
        dice = Dice(t, f, r, l, b, u)
    else:
        a = list(map(int, input().split()))
        dice.a.append(a)
dice.dice_tuple()
s='SNEWUU'
try:
    for u in range(n - 1):
        for t in range(u + 1, n):
            for j in s:
                for r in range(4):
                    if dice.a[t] == dice.a[u]:
                        raise Exception
                    dice.roll_w(t)
                dice.roll2(j, t)
    else:
        print('Yes')
except:
    print('No')
N, K = map(int, input().split())
R, S, P = map(int, input().split())
score = {'r': R, 's': S, 'p': P}
T = input()
hand = {'r': 'p', 's': 'r', 'p': 's'}

history = ['a'] * (2 * N)
cnt = 0
for i, t in enumerate(T):
    l = i - K
    r = i + K
    u = hand[t]
    flag = 1

    if u == history[l]:
        u = T[r]
        flag = 0

    cnt += score[u] * flag
    history[i] = u

print(cnt)


### FIXED CODE ###

N, K = map(int, input().split())
R, S, P = map(int, input().split())
T = input()

score = {'p': P, 'r': R, 's': S}
hand = {'r': 'p', 's': 'r', 'p': 's'}
H = [' '] * N

ans = 0
for sub in range(K):
    i = sub
    while i < N:
        c = T[i]
        if i - K >= 0 and hand[c] == H[i - K]:
            s = 0
            if i + K < N and hand[T[i + K]] == c:
                H[i] = hand[hand[c]]
            else:
                H[i] = c
        else:
            H[i] = hand[c]
            s = score[hand[c]]
        i += K
        ans += s

print(ans)