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

import sys
def input():
    return sys.stdin.readline()[:-1]
import math
from itertools import permutations, combinations
from collections import deque

sys.setrecursionlimit(10**7)
N = int(input())
ab = []

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

#print(edges)
#print(ab)
K = max(list(map(len, edges)))
print(K)

adjm = [[0]*N for _ in range(N)]

# BFS
Q = deque()
for edge in edges[0]:
    Q.append((0, edge))

while Q:
    labels = set([i for i in range(1, K+1)])
    points = Q.popleft()
    #print(points)
    p0, p1 = points

    set1 = set(adjm[p0])
    set2 = set(adjm[p1])
    can_use_labels = labels - (set1 | set2)
    #print('can use:', can_use_labels)
    use_label = can_use_labels.pop()
    #print('use:', use_label)

    adjm[p0][p1] = use_label
    adjm[p1][p0] = use_label

    for p in edges[p1]:
        if adjm[p1][p] == 0:
            Q.append((p1, p))

for abi in ab:
    a, b = abi[0], abi[1]
    print(adjm[a-1][b-1])


### FIXED CODE ###

#import numpy as np
from copy import deepcopy
import sys, math
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial#, gcd
from bisect import bisect_left #bisect_left(list, value)
sys.setrecursionlimit(10**7)
enu = enumerate
MOD = 10**9+7
def input(): return sys.stdin.readline()[:-1]
def pri(x): print('\n'.join(map(str, x)))

N = int(input())
#AB = [list(map(int, input().split())) for _ in range(N-1)]
edge = [[] for i in range(N)]
seen = [0]*N
colors = [0]*(N-1)
#for val in AB:
for i in range(N-1):
    x, y = list(map(int, input().split()))
    edge[x-1].append((i, y-1))
    edge[y-1].append((i, x-1))

lenedge = [len(val) for val in edge]
mledge = max(lenedge)
root = lenedge.index(mledge)
print(mledge)


que = deque()
que.append([(root, -1)])
while que[0]:
    nque = []
    cque = que.popleft()
    #print('cstack:', cstack)
    for tupl in cque:
        index, pcolor = tupl
        seen[index] = 1
        color = 1
        if (color == pcolor): color += 1
        for tupl2 in edge[index]:
            findex, edgei = tupl2
            if seen[edgei] == 0:
                nque.append((edgei, color))
                colors[findex] = color
                color += 1
                if (color == pcolor): color += 1
    que.append(nque)

#pri(color)

for color in colors:
    print(color)
### BUGGY CODE ###

import numpy as np
n = int(input())
A = list(map(int,input().split()))
q = int(input())
B = []
C = []
for _ in range(q):
    b,c = map(int,input().split())
    B.append(b)
    C.append(c)
B = np.array(B)
C = np.array(C)
for i in range(q):
    A = [C[i] if a == B[i] else a for a in A]
    print(sum(A))

### FIXED CODE ###

import numpy as np
n = int(input())
A = list(map(int,input().split()))
q = int(input())
ar = np.zeros(10**5+1)
for i in A:
    ar[i] += 1
ans = np.sum(A) 
for _ in range(q):
    b,c = map(int,input().split())
    ans = ans+ar[b]*(c-b)
    ar[c] += ar[b]
    ar[b] = 0
    print(int(ans))
### BUGGY CODE ###

s = input()

l = [0 for i in range(2019)]
n = len(s)
l[0] = 1

for i in range(n):
    tmp = int(s[i:]) % 2019 
    l[tmp] += 1

res = 0
for i in l:
    res += i * (i-1) // 2

print(res)

### FIXED CODE ###

s = input()
s = s[::-1]

l = [0 for i in range(2019)]
n = len(s)
l[0] = 1
prev = 0
digit = 1
for c in s:
    t = (int(c) * digit + prev) % 2019
    l[t] += 1
    digit = (digit * 10) % 2019
    prev = t

res = 0
for i in l:
    res += i * (i-1) // 2

print(res)
### BUGGY CODE ###

import os, sys, re, math

(N, Q) = [int(n) for n in input().split()]
S = input()

L = []
R = []
for i in range(Q):
    (l, r) = [int(n) for n in input().split()]
    L.append(l - 1)
    R.append(r)

memo = {}
for i in range(Q):
    if (L[i], R[i]) in memo:
        print(memo[(L[i], R[i])])
        continue
    substr = S[L[i]:R[i]]
    replaced = substr.replace('AC', '')
    oc = (len(substr) - len(replaced)) >> 1
    print(oc)
    memo[(L[i], R[i])] = oc


### FIXED CODE ###

import os, sys, re, math

(N, Q) = [int(n) for n in input().split()]
S = input()

oc = [0]
n = 0
for i in range(len(S) - 1):
    if S[i:i+2] == 'AC':
        n += 1
    oc.append(n)
oc.append(n)

for i in range(Q):
    (l, r) = [int(n) for n in input().split()]
    print(oc[r - 1] - oc[l - 1])
### BUGGY CODE ###

s = " " + input()
t = " " + input()

dp = [[[] for _ in range(len(t))] for _ in range(2)]

import copy

for i in range(1, len(s)):
    i0 = i % 2
    i1 = (i - 1) % 2
    for j in range(1, len(t)):
        if len(dp[i1][j]) > len(dp[i0][j - 1]):
            dp[i0][j] = dp[i1][j]
        else:
            dp[i0][j] = copy.copy(dp[i0][j - 1])
        if s[i] == t[j]:
            if len(dp[i1][j - 1]) + 1 > len(dp[i0][j]):
                dp[i1][j - 1].append(s[i])
                dp[i0][j] = dp[i1][j - 1]
print("".join(dp[(len(s) - 1) % 2][len(t) - 1]))

### FIXED CODE ###

s = " " + input()
t = " " + input()

dp = [[0 for _ in range(len(t))] for _ in range(len(s))]

for i in range(1, len(s)):
    for j in range(1, len(t)):
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + (1 if s[i] == t[j] else 0))

i = len(s) - 1
j = len(t) - 1
c = 0
ans = []
ansl = dp[-1][-1]
while (c < ansl):
    if s[i] == t[j]:
        ans.append(s[i])
        i -= 1
        j -= 1
        c += 1
    else:
        if dp[i - 1][j] > dp[i][j - 1]:
            i -= 1
        else:
            j -= 1
ans.reverse()
print("".join(ans))
### BUGGY CODE ###

import numpy as np
S=input()
T=input()
dp=[["" for j in range(len(T))] for i in range(len(S))]
for i in range(len(S)):
    for j in range(len(T)):
        if S[i]==T[j]:
            dp[i][j]=dp[max(i-1,0)][max(j-1,0)]+S[i]
        else:
            L=[dp[max(i-1,0)][max(j-1,0)],dp[max(i-1,0)][j],dp[i][max(j-1,0)]]
            dp[i][j]=L[np.argmax([len(L[0]),len(L[1]),len(L[2])])]
print(dp[-1][-1])

### FIXED CODE ###

S=input()
T=input()
dp=[[0 for j in range(len(T)+1)] for i in range(len(S)+1)]
for i in range(len(S)):
    for j in range(len(T)):
        if S[i]==T[j]:
            dp[i+1][j+1]=dp[i][j]+1
        dp[i+1][j+1]=max(dp[i+1][j+1],dp[i+1][j])
        dp[i+1][j+1]=max(dp[i+1][j+1],dp[i][j+1])
i=len(S)
j=len(T)
ans=""
while i>0 and j>0:
    if dp[i][j]==dp[i-1][j]:
        i-=1
    elif dp[i][j]==dp[i][j-1]:
        j-=1
    else:
        ans=S[i-1]+ans
        i-=1
        j-=1
print(ans)
### BUGGY CODE ###

a, b, c = map(int, input().split())
ans = 0
while a % 2 == 0 and b % 2 == 0 and c % 2 == 0:
    total = a + b + c
    a = (total - a) / 2
    b = (total - b) / 2
    c = (total - c) / 2
    ans += 1
print(ans)

### FIXED CODE ###

a, b, c = map(int, input().split())
ans = 0
if a == b == c and a % 2 == 0:
    print(-1)
else:
    while a % 2 == 0 and b % 2 == 0 and c % 2 == 0:
        total = a + b + c
        a = (total - a) / 2
        b = (total - b) / 2
        c = (total - c) / 2
        ans += 1
    print(ans)
### BUGGY CODE ###

#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

def main():
    H, W, M = map(int, input().split())
    h = [0] * H
    w = [0] * W
    hw = set()
    for i in range(M):
        tmph, tmpw = map(lambda x: int(x)-1, input().split())
        h[tmph] += 1
        w[tmpw] += 1
        hw.add((tmph,tmpw))
    # 最大値より1だけ小さくなる物が候補
    HMAX = max(h)
    hcandidate = []
    for i in range(H):
        if h[i] >= HMAX-1:
            hcandidate.append(i)

    WMAX = max(w)
    wcandidate = []
    for i in range(W):
        if w[i] >= WMAX-1:
            wcandidate.append(i)

    ans = 0
    for i in hcandidate:
        for j in wcandidate:
            res = h[i]+w[j]
            if (i,j) in hw:
                res -= 1
            ans = max(ans, res)
    print(ans)

if __name__ == "__main__":
    main()

### FIXED CODE ###

#!/usr/bin/env python3
import sys
from collections import deque, Counter
from heapq import heappop, heappush
from bisect import bisect_right
from itertools import accumulate

sys.setrecursionlimit(10**6)
INF = 10**12
m = 10**9 + 7

def main():
    H, W, M = map(int, input().split())
    h = [0] * H
    w = [0] * W
    hw = set()
    for i in range(M):
        tmph, tmpw = map(lambda x: int(x)-1, input().split())
        h[tmph] += 1
        w[tmpw] += 1
        hw.add((tmph,tmpw))
    # 最大値より1だけ小さくなる物が候補
    HMAX = max(h)
    h0 = []
    for i in range(H):
        if h[i] == HMAX:
            h0.append(i)

    WMAX = max(w)
    w0 = []
    for i in range(W):
        if w[i] == WMAX:
            w0.append(i)

    ans = 0
    for i in h0:
        for j in w0:
            if (i,j) not in hw:
                print(HMAX+WMAX)
                exit()
    print(HMAX+WMAX-1)

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

N,M = map(int, input().split())
g = {}
for v in range(1,N*3+1):
    g[v] = []

for _ in range(M):
    u,v = map(int, input().split())
    g[u + N*0].append(v + N*1)
    g[u + N*1].append(v + N*2)
    g[u + N*2].append(v + N*0)

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

visiting = [(S,0)]
visited = {}
while visiting:
    v,d = visiting.pop(0)
    if v in visited:
        continue
    visited[v] = d
    if v == T:
        break

    for v2 in g[v]:
        if v2 in visiting or v2 in visited:
            continue
        visiting.append((v2,d+1))

if T in visited:
    print(visited[T] // 3)
else:
    print(-1)


### FIXED CODE ###

N, M = map(int, input().split())
E = {}
for v in range(1,N*3+1):
    E[v] = []
for _ in range(M):
    u, v = map(int, input().split())
    E[u].append(v+N)
    E[u+N].append(v+2*N)
    E[u+2*N].append(v)
S, T = map(int, input().split())
dist = [-1 for _ in range(3*N)]
dist[S-1] = 0

q = []
q.append(S)
while q:
    n = q.pop(0)
    if n == T:
        break
    for t in E[n]:
        if dist[t-1] < 0:
            dist[t-1] = dist[n-1] + 1
            q.append(t)
print(-1 if dist[T-1] < 0 else dist[T-1] // 3)
### BUGGY CODE ###

def binarySearch(e, n, A):
    if e<A[0] or e>A[-1]:
        return False
    l, r = 0, n
    while l<=r:
        m = (r+l)//2
        if e == A[m]:
            return True
        elif e < A[m]:
            r = m
        else:
            l = m
    return False


def ALDS1_4B():
    n, S = int(input()), list(map(int, input().split()))
    q, T = int(input()), list(map(int, input().split()))
    print(sum([1 for e in T if binarySearch(e, n, S)]))


if __name__ == '__main__':
    ALDS1_4B()

### FIXED CODE ###

def binarySearch(e, n, A):
    if e<A[0] or e>A[-1]:
        return False
    l, r = 0, n
    while l<=r:
        m = (r+l)//2
        if m == r or m == l:
            return False
        if e == A[m]:
            return True
        elif e < A[m]:
            r = m
        else:
            l = m
    return False


def ALDS1_4B():
    n, S = int(input()), list(map(int, input().split()))
    q, T = int(input()), list(map(int, input().split()))
    print(sum([1 for e in T if binarySearch(e, n, S)]))


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

N=int(input())

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

min=50000000000

for i in range(N-1):
    sunuke=sum(a[0:i+1])
    arai=sum(a[i+1:N])
    if(abs(sunuke-arai)<=min):
        min=abs(sunuke-arai)

print(min)

### FIXED CODE ###

n = int(input())
a = [int(x) for x in input().split()]
sm = sum(a)//2
mn = 10**10
ar = a[0]
su = a[-1]
for i in range(1,len(a)-1):
    if abs(sm - ar) < mn:
        mn = abs(sm - ar)
        mn_i = i
    ar += a[i]

if len(a) > 2:
    ar = sum(a[0:mn_i])
    su = sum(a[mn_i::])
    print(abs(ar-su))
else:
    print(abs(ar-su))
### BUGGY CODE ###

# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))

from collections import Counter
N = int(input())
src = list(map(int,input().split()))

ctr1 = Counter()
ctr2 = Counter()

ans = []
for i in range(N):
    ctr1[src[i]] += 1
    ctr2[src[-i-1]] += 1
    for k, v in ctr1.items():
        if ctr2[k] != v:
            break
    else:
        ans.append(i+1)

print(*ans)


### FIXED CODE ###

# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
from bisect import bisect_left, insort

N = int(input())
src = list(map(int,input().split()))
ans = []
A, B = [], []

for i in range(N):
    a, b = src[i], src[-i-1]
    addA = False
    addB = False

    if a != b:
        j = bisect_left(B, a)
        if j != len(B) and B[j] == a:
            del B[j]
        else:
            addA = True

        j = bisect_left(A, b)
        if j != len(A) and A[j] == b:
            del A[j]
        else:
            insort(B, b)

        if addA:
            insort(A, a)

    if len(A) == len(B) == 0:
        ans.append(i+1)

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

k, s = map(int, input().split())
cnt=0

for x in range(0,k+1):
    for y in range(0,k+1):
        for z in range(0,k+1):
            if x+y+z==s:
                cnt+=1

print(cnt)

### FIXED CODE ###

k, s = map(int, input().split())
cnt=0

for x in range(0,k+1):
    for y in range(0,k+1):
        z = s - x - y
        if 0 <= z and z <= k:
            cnt+=1

print(cnt)
### BUGGY CODE ###

n = int(input())
num_list =  list(map(int,input().split()))
num_list2 = [0]*n
num_list3 = [0]*n
for i in num_list:
    num_list2[i-1] += 1
for i in range(n):
    ans = num_list2[i]*(num_list2[i]-1)//2
    num_list3[i] = ans

for i in num_list:
    ans = (num_list2[i-1]-1)*(num_list2[i-1]-2)//2
    print(ans-num_list3[i-1]+sum(num_list3))

### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
cnt = [0] * n
for i in a:
    cnt[i-1] += 1
total = [0] * n
for i in range(n):
    total[i] = cnt[i] * (cnt[i] - 1) // 2
total_sum = sum(total)
for i in a:
    ans = total_sum
    ans -= total[i-1]
    ans += (cnt[i-1] - 1) * (cnt[i-1] - 2) // 2
    print(ans)
### BUGGY CODE ###

def main():
    n, k, l = map(int, input().split())
    roads = [set([i]) for i in range(n)]
    rails = [set([i]) for i in range(n)]
    for _ in range(k):
        a, b = map(lambda s: int(s) - 1, input().split())
        i_large, i_small = (a, b) if len(roads[a]) > len(roads[b]) else (b, a)
        roads[i_large] |= roads[i_small]
        for i in roads[i_small]:
            roads[i] = roads[i_large]
    for _ in range(l):
        a, b = map(lambda s: int(s) - 1, input().split())
        i_large, i_small = (a, b) if len(rails[a]) > len(rails[b]) else (b, a)
        rails[i_large] |= rails[i_small]
        for i in rails[i_small]:
            rails[i] = rails[i_large]
    res = [len(roads[i] & rails[i]) for i in range(n)]
    print(' '.join(map(str, res)))


if __name__ == '__main__':
    main()


### FIXED CODE ###

def root(parents, i):
    if parents[i] == i:
        return i
    parents[i] = root(parents, parents[i])
    return parents[i]


def main():
    n, k, l = map(int, input().split())
    roads = list(range(n))
    rails = list(range(n))

    for _ in range(k):
        a, b = map(lambda s: int(s) - 1, input().split())
        root_a, root_b = root(roads, a), root(roads, b)
        if root_a != root_b:
            roads[root_a] = root_b

    for _ in range(l):
        a, b = map(lambda s: int(s) - 1, input().split())
        root_a, root_b = root(rails, a), root(rails, b)
        if root_a != root_b:
            rails[root_a] = root_b

    counts = dict()
    for i in range(n):
        pair = (root(roads, i), root(rails, i))
        if not pair in counts:
            counts[pair] = 1
        else:
            counts[pair] += 1

    print(' '.join(
        map(lambda i: str(counts[(root(roads, i), root(rails, i))]), range(n))))


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

import sys


N = int(input())
X_bin = input()

X_int = int(X_bin, 2)  # bin => int
# popcount
X_cnt = 0
for i in X_bin:
    if i == '1':
        X_cnt += 1
dp = {0: 0, 1: 1}
bin_cnt = {}

sys.setrecursionlimit(10**5)


def calc_bin_cnt(next_int):
    if next_int in bin_cnt.keys():
        cnt = bin_cnt[next_int]
    else:
        next_bin = format(next_int, 'b')
        # popcount
        cnt = 0
        for i in next_bin:
            if i == '1':
                cnt += 1
    return cnt


def calc_ans(next_int, cnt=None):
    key = next_int
    if key in dp.keys():
        ans = dp[key]
    else:
        if cnt is None:
            cnt = calc_bin_cnt(next_int)
        else:
            bin_cnt[key] = cnt
        bin_cnt[next_int] = cnt
        next_int = next_int % cnt
        if next_int > 0:
            ans = calc_ans(next_int) + 1
        else:
            ans = 1
        dp[key] = ans
    return ans


for i in range(N):
    if X_bin[i] == '1':
        print(calc_ans(X_int - (2**(N-i-1)), X_cnt-1))
    else:
        print(calc_ans(X_int + (2**(N-i-1)), X_cnt+1))


### FIXED CODE ###

N = int(input())
X_bin = input()
#N = 10**5
#X_bin = '1' * N

X_int = int(X_bin, 2)  # bin => int


def calc_bin_cnt(next_int):
    if next_int == 0:
        return 0
    next_bin = bin(next_int)[2:]
    # popcount
    cnt = 0
    for i in next_bin:
        if i == '1':
            cnt += 1
    return cnt


def calc_ans(next_int, cnt=None):
    if next_cnt == 0:
        return 0
    if next_int == 0:
        return 1
    if cnt is None:
        cnt = calc_bin_cnt(next_int)
    next_int = next_int % cnt
    if next_int > 0:
        ans = calc_ans(next_int) + 1
    else:
        ans = 1
    return ans


X_cnt = calc_bin_cnt(X_int)
f_p = X_int % (X_cnt+1)
if X_cnt-1 == 0:
    f_m = 0
else:
    f_m = X_int % (X_cnt-1)
for i in range(N):
    if X_bin[i] == '1':
        next_cnt = X_cnt - 1
        if next_cnt == 0:
            next_int = 0
        else:
            next_int = f_m - pow(2, (N-i-1), next_cnt)  # マイナスでも正しく求まる？
    else:
        next_cnt = X_cnt + 1
        next_int = f_p + pow(2, (N-i-1), next_cnt)
    print(calc_ans(next_int, next_cnt))
    #calc_ans(next_cnt, next_cnt)
### BUGGY CODE ###

def answer(X, Y):
    m, n = len(X), len(Y)
    C = [[0] * (n + 1) for _ in range(m + 1)]
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if X[i - 1] == Y[j - 1]:
                C[i][j] = C[i-1][j-1] + 1
            else:
                C[i][j] = max(C[i][j -1], C[i -1][j])
    return C[m][n]


if __name__ == '__main__':
    q = int(input())
    for _ in range(q):
        X = input()
        Y = input()
        print(answer(X, Y))

### FIXED CODE ###

def lcs_better(X, Y):
    costs = [0]
    for c in Y:
        for i in range(len(costs) - 1, -1, -1):
            tmp = X.find(c, costs[i])
            if tmp + 1:
                if i + 1 < len(costs):
                    costs[i + 1] = min(costs[i + 1], tmp + 1)
                else:
                    costs.append(tmp + 1)
    return len(costs) - 1


def answer(X, Y):
    m, n = len(X), len(Y)
    C = [[0] * (n + 1) for _ in range(m + 1)]
    for i in range(1, m + 1):
        for j in range(1, n + 1):
            if X[i - 1] == Y[j - 1]:
                C[i][j] = C[i-1][j-1] + 1
            else:
                C[i][j] = max(C[i][j -1], C[i -1][j])
    return C[m][n]


if __name__ == '__main__':
    q = int(input())
    for _ in range(q):
        X = input()
        Y = input()
        print(lcs_better(X, Y))
### BUGGY CODE ###

# @author 

import sys

class OMatching:
    def solve(self):
        from collections import defaultdict

        MOD = 10 ** 9 + 7

        n = int(input())
        a = []
        for i in range(n):
            a.append([int(_) for _ in input().split()])

        dp = [[0] * ((1 << n) + 1) for _ in range(n + 1)]
        dp[0][0] = 1
        by_1 = defaultdict(set)
        for j in range(1 << n):
            by_1[bin(j).count('1')].add(j)
        for i in range(1, n + 1):
            for j in by_1[i - 1]:
                for k in range(n):
                    if (j >> k) & 1 or a[i - 1][k] == 0:
                        continue
                    dp[i][j | (1 << k)] += dp[i - 1][j]
                    dp[i][j | (1 << k)] %= MOD

        # [print(item) for item in dp]

        print(sum(dp[n]))

solver = OMatching()
input = sys.stdin.readline

solver.solve()


### FIXED CODE ###

# @author 

import sys

class OMatching:
    def solve(self):
        from collections import defaultdict

        def numberOfSetBits(i):
            i = i - ((i >> 1) & 0x55555555)
            i = (i & 0x33333333) + ((i >> 2) & 0x33333333)
            return (((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) & 0xffffffff) >> 24

        MOD = 10 ** 9 + 7

        n = int(input())
        a = []
        for i in range(n):
            a.append([int(_) for _ in input().split()])


        dp = [[0] * ((1 << n) + 1) for _ in range(n + 1)]
        dp[0][0] = 1
        by_1 = defaultdict(list)
        for j in range(1 << n):
            by_1[numberOfSetBits(j)].append(j)
        # for j in range(1 << n):
        #     by_1[bin(j).count('1')].append(j)

        for i in range(1, n + 1):
            for j in by_1[i - 1]:
                for k in range(n):
                    if (j >> k) & 1 or a[i - 1][k] == 0:
                        continue
                    dp[i][j | (1 << k)] += dp[i - 1][j]
                    dp[i][j | (1 << k)] %= MOD

        # [print(item) for item in dp]

        print(sum(dp[n]))

solver = OMatching()
input = sys.stdin.readline

solver.solve()
### BUGGY CODE ###

import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

N, M = map(int, input().split())
X, Y, Z = [0] * N, [0] * N, [0] * N

for i in range(N):
    X[i], Y[i], Z[i] = map(int, input().split())

codes = [[1, 1, 1], [1, 1, -1], [1, -1, 1], [1, -1, -1], [-1, 1, 1], [-1, 1, -1], [-1, -1, 1], [-1, -1, -1]]


def calc(code, idx):
    return code[0] * X[idx] + code[1] * Y[idx] + code[2] * Z[idx]


ans = 0
INF = 1 << 60
for code in codes:
    dp = [-INF] * (M + 1)
    dp[0] = 0
    for i in range(N):
        tmp = [-INF] * (M + 1)
        tmp[0] = 0
        for j in range(M, 0, -1):
            if dp[j - 1] >= 0:
                tmp[j] = max(dp[j], dp[j - 1] + calc(code, i))

        dp = tmp
    #     print("", dp)
    # print(dp)
    ans = max(ans, dp[M])

print(ans)

### FIXED CODE ###

import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

N, M = map(int, input().split())
X, Y, Z = [0] * N, [0] * N, [0] * N

for i in range(N):
    X[i], Y[i], Z[i] = map(int, input().split())

codes = [[1, 1, 1], [1, 1, -1], [1, -1, 1], [1, -1, -1], [-1, 1, 1], [-1, 1, -1], [-1, -1, 1], [-1, -1, -1]]


def calc(code, idx):
    return code[0] * X[idx] + code[1] * Y[idx] + code[2] * Z[idx]


INF = 1 << 60
ans = -INF
for code in codes:
    ar = [0] * N

    for i in range(N):
        ar[i] = calc(code, i)

    ar.sort(reverse=True)

    ans = max(ans, sum(ar[:M]))

print(ans)