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

N = int(input())
S = [list(input()) for _ in range(N)]
for i in range(N):
    S[i] = sorted(S[i])

count = 0
for i in range(N):
    count += S.count(S[i]) - 1

print(count // 2)

### FIXED CODE ###

N = int(input())
dic = {}
s = 0
for n in range(N):
  v = ''.join(sorted(input()))
  if dic.get(v) is None:
    dic[v] = 1
  else:
    s += dic[v]
    dic[v] += 1
print(s)
### BUGGY CODE ###

n,k=map(int,input().split())
A=list(map(int,input().split()))
A_unq=set(A)
b=[]
for i in A_unq:
       b.append(A.count(i))
b.sort()
print('0' if k>=len(A_unq) else sum(b[:len(A_unq)-k]))

### FIXED CODE ###

N, K = map(int, input().split())
A = list(map(int, input().split()))
B = [0 for i in range(N + 1)]

for a in A:
    B[a] += 1
nums = [b for b in B if b > 0]
nums.sort()
n = len(nums)
print(0 if n<=K else sum(nums[:n - K]))
### BUGGY CODE ###

import sys
input = sys.stdin.readline

N, W = map(int, input().split())
dp = [0] * (W + 1)

for _ in range(N):
    w, v = map(int, input().split())
    for i in range(W, w - 1, -1):
        dp[i] = max(dp[i], dp[i - w] + v)

print (dp[W])

### FIXED CODE ###

import sys
input = sys.stdin.readline

N, W = map(int, input().split())
dp = [0] * (W + 1)

for _ in range(N):
    w, v = map(int, input().split())
    for i in range(W, w - 1, -1):
        dp[i] = max(dp[i], dp[i - w] + v)

print (dp[W])
### BUGGY CODE ###

from math import sqrt
from collections import Counter

def arrayF(n): 
    F=[0]*(n+1) 
    i=2 
    while i*i<=n:
        if F[i] == 0: 
            k=i*i
            while k <= n:
                if F[k] == 0:
                    F[k] = i
                k += i
        i += 1 
    return F


def factorization(x, F): 
    primeFactors = []
    while F[x] > 0:
        primeFactors.append(F[x])
        x //= F[x] 
    if x != 1:
        primeFactors.append(x)
    return set(primeFactors)

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

amax = max(A)
F  = arrayF(amax)

P = Counter()
for a in A:
    p = Counter(factorization(a, F))
    P += p

p_max = max(P.values()) if P else 1
if  p_max == 1:
    print('pairwise coprime')
elif  p_max <  n:
    print('setwise coprime')
else:
    print('not coprime')

### FIXED CODE ###

def arrayF(n): 
    F=[0]*(n+1) 
    i=2 
    while i*i<=n:
        if F[i] == 0: 
            k=i*i
            while k <= n:
                if F[k] == 0:
                    F[k] = i
                k += i
        i += 1 
    return F


def factorization(x, F): 
    primeFactors = []
    while F[x] > 0:
        primeFactors.append(F[x])
        x //= F[x] 
    if x != 1:
        primeFactors.append(x)
    return set(primeFactors)

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

amax = max(A)
F  = arrayF(amax)

P = [0] *  (amax+1)
for a in A:
    for p in factorization(a,  F):
        P[p] += 1
p_max = max(P)
if  p_max <= 1:
    print('pairwise coprime')
elif  p_max <  n:
    print('setwise coprime')
else:
    print('not coprime')
### BUGGY CODE ###

l,r = map(int,input().split())
mods = l*r
for i in range(l,r+1):
    for j in range(l,i+1):
        if i == j:
            continue
        else:
            q, mod = divmod(i*j, 2019)
            mods = min(mod,mods)
print(mods)

### FIXED CODE ###

l,r = map(int,input().split())
r = min(r,l+678)
ans = 2018

for i in range(l,r+1):
    for j in range(i+1,r+1):
        ans = min(i*j%2019,ans)
print(ans)
### BUGGY CODE ###

N, x = map(int, input().split())
a = list(map(int, input().split()))
b = []
cnt = 0
for i in a:
    if i>x:
        tmp = abs(x-i)
        i-=tmp
        cnt+=tmp
    b.append(i)
for j in range(N-1):
    while b[j] + b[j+1] > x:
        b[j+1]-=1
        cnt+=1
print(cnt)

### FIXED CODE ###

N, x = map(int, input().split())
a = [0] + list(map(int, input().split()))
cnt = 0
for i in range(N):
    if a[i]+a[i+1]>x:
        tmp = a[i]+a[i+1]-x
        a[i+1] -= tmp
        cnt+=tmp
print(cnt)
### BUGGY CODE ###

n = int(input())
s = list(input())
tot = 0
for i in range(n):
    for j in range(i + 1, n):
        if s[i] != s[j]:
            li = ['R', 'G', 'B']
            li.remove(s[i])
            li.remove(s[j])
            tot += s[j + 1:].count(li[0])
            if n > 2 * j - i and s[2 * j - i] == li[0]:
                tot -= 1

print(tot)


### FIXED CODE ###

n = int(input())
s = list(input())
tot = 0
tot += s.count('R') * s.count('G') * s.count('B')
for i in range(n):
    for j in range(i + 1, n):
        k = 2 * j - i
        if k < n and s[i] != s[j] and s[j] != s[k] and s[k] != s[i]:
            tot -= 1
print(tot)
### BUGGY CODE ###

n = int(input())
s = input()
r = []
g = []
b = []

for i in range(n):
  if s[i] == 'R':
    r.append(i)
  elif s[i] == 'G':
    g.append(i)
  else:
    b.append(i)
res = 0
if max(len(r),len(g),len(b)) == len(g):
  for i in r:
    for j in b:
      flg = 0
      if (i + j) / 2 in g:
        flg += 1
      if 2 * i - j in g:
        flg += 1
      if 2 * j - i in g:
        flg += 1
      res += len(g) - flg
else:
  for i in g:
    for j in r:
      flg = 0
      if (i + j) / 2 in b:
        flg += 1
      if 2 * i - j in b:
        flg += 1
      if 2 * j - i in b:
        flg += 1
      res += len(b) - flg
print(res)


### FIXED CODE ###

n=int(input())
s=input()
R=[]
G={}
B=[]
for i in range(n):
  if s[i]=='R':
    R.append(i+1)
  elif s[i]=='G':
    G[i+1]=G.get(i+1,0)+1
  else:
    B.append(i+1)
res=len(R)*len(G)*len(B)
for i in R:
  for j in B:
    if ((i+j)%2==0 and G.get((i+j)//2,0)==1):
      res-=1
    if G.get(2*max(i,j)-min(i,j),0)==1:
      res-=1
    if G.get(2*min(i,j)-max(i,j),0)==1:
      res-=1
print(res)
### BUGGY CODE ###

#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7
n=I()
lis=LI()
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
lists=[prime_factorize(lis[i]) for i in range(n)]
C=Counter([])
for i in range(n):
    x=Counter(lists[i])
    for j in x.keys():
        C[j]=max(C[j],x[j])
#print(C)

ans=0
for i in range(n):
    x=Counter(lists[i])
    y=1
    for j in C.keys():
        y=(y*pow(j,C[j]-x[j],mod))%mod
    #print(y%mod)
    ans=(ans+y)%mod
print(ans%mod)
        
        
    
    
        
    
    


### FIXED CODE ###

#float型を許すな
#numpyはpythonで
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

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

def lcm(a, b):
    return a // gcd(a, b) * b
#print(lcm(lcm(1000000,999999),999998))
#print(999998//2*999999)


#print(1000000*999999*999998%mod)
n=I()
lis=LI()
promod=1
LCM=1
for i in range(n):
    LCM=lcm(lis[i],LCM)
#print(LCM)
for i in range(n):
    promod=(promod*(lis[i]%mod))%mod
smmod=0
for i in range(n):
    smmod=(smmod+promod*pow(lis[i],mod-2,mod))%mod
#print(smmod)
x=(promod*pow(LCM,mod-2,mod))%mod
ans=(smmod*pow(x,mod-2,mod))%mod
print(ans)
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
for i in range(N):
    print(A.count(i+1))

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
c = {i:0 for i in range(1,N+1)}
for i in range(N-1):
    c[A[i]] += 1
for i in range(1,N+1):
    print(c[i])
### BUGGY CODE ###

X,Y,Z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))

C.sort(reverse=True)
AB = [0 for _ in range(X*Y)]
ctr = 0
for x in range(X):
    for y in range(Y):
        AB[ctr] = A[x]+B[y]
        ctr += 1
AB.sort(reverse=True)
ABC = [0 for _ in range(min(len(AB),K)*Z)]
ctr = 0
for ab in range(min(len(AB),K)):
    for z in range(Z):
        ABC[ctr] = AB[ab]+C[z]
        ctr += 1
ABC.sort(reverse=True)
for k in range(K):
    print(ABC[k])

### FIXED CODE ###

import heapq
import numpy as np
X,Y,Z,K = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
C = list(map(int,input().split()))
flag = np.ones((X,Y,Z),dtype=np.bool)
A.sort(reverse=True)
B.sort(reverse=True)
C.sort(reverse=True)

ABC = []
heapq.heappush(ABC,(-1 * (A[0]+B[0]+C[0]),0,0,0))
flag[0][0][0] = False
ctr = 0
while ctr < K and ABC:
    s,a,b,c = heapq.heappop(ABC)
    print(-s)
    if a+1<X and flag[a+1][b][c]:
        heapq.heappush(ABC,(-1 * (A[a+1]+B[b]+C[c]),a+1,b,c))
        flag[a+1][b][c] = False
    if b+1<Y and flag[a][b+1][c]:
        heapq.heappush(ABC,(-1 * (A[a]+B[b+1]+C[c]),a,b+1,c))
        flag[a][b+1][c] = False
    if c+1<Z and flag[a][b][c+1]:
        heapq.heappush(ABC,(-1 * (A[a]+B[b]+C[c+1]),a,b,c+1))
        flag[a][b][c+1] = False
    ctr += 1
### BUGGY CODE ###

from sys import exit

N, M = map(int, input().split())

_a = -1
A = []
ans = 0

for _ in range(M):
    a_ = int(input())
    if _a + 1 == a_:
        print(0)
        exit()
    _a = a_
    A.append(_a)

step = [0] * (N + 1)

step[0] = 1

for s in range(1, N + 1):
    if s in A:
        step[s] = 0
        continue
    if s == 1:
        step[s] = 1
        continue
    else:
        step[s] = (step[s - 1] + step[s - 2]) % 1000000007

print(step[N])


### FIXED CODE ###

N, M = map(int, input().split())

A = [False] * (N + 1)

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

step = [0] * (N + 1)

step[0] = 1

for s in range(1, N + 1):
    if A[s]:
        step[s] = 0
        continue
    if s == 1:
        step[s] = 1
        continue
    else:
        step[s] = step[s - 1] + step[s - 2]
        step[s] %= 1000000007

print(step[N])
### BUGGY CODE ###

import itertools
ans = 10000000000000000000000000000000000
n, k = map(int, input(). split())
p = list(map(int, input(). split()))
P = list(itertools.combinations(p, k))
s = len(P)
for i in range(s):
  P[i] = list(P[i])
  q = sum(P[i])
  if ans >= q:
    ans = q
print(ans)

### FIXED CODE ###

n, k = map(int, input(). split())
p = list(map(int, input(). split()))
p = sorted(p)
ans = 0
for i in range(k):
  ans += p[i]
print(ans)
### BUGGY CODE ###

N, M  = list(map(int, input().split()))
L = []
R = []

for i in range(M):
    L_in, R_in = list(map(int, input().split()))
    L.append(L_in)
    R.append(R_in)

cnt = 0
for n in range(N):
    counter = 0
    for m in range(M):
        if (n + 1) == R[m] or (L[m] <= (n + 1) and (n + 1) <= R[m]):
            counter += 1
            if counter == M:
                cnt += 1
        else:
            break
print(cnt)


### FIXED CODE ###

N, M  = list(map(int, input().split()))
L = []
R = []

for m in range(M):
    L_in, R_in = list(map(int, input().split()))
    L.append(L_in)
    R.append(R_in)

if min(R) >= max(L):
    print(min(R) - max(L) + 1)
else:
    print(0)
### BUGGY CODE ###

def isOdd(num):
	if num % 2 == 0:
		return False
	else:
		return True

A, B, C = map(int, raw_input().split())
[A, B, C] = sorted([A, B, C])
ans = 0
while not isOdd(A) and not isOdd(B) and not isOdd(C):
	ans += 1
	new1, new2, new3 = B/2 + C/2, A/2 + C/2, A/2 + B/2
	if new1 == new3:
		ans = -1
	else:
		A, B, C = new1, new2, new3
print ans


### FIXED CODE ###

A, B, C = map(int, raw_input().split())
[A, B, C] = sorted([A, B, C])
ans = 0
while A%2==0 and B%2==0 and C%2==0:
	ans += 1
	new1, new2, new3 = B/2 + C/2, A/2 + C/2, A/2 + B/2
	if new1 == new3:
		ans = -1
		break
	else:
		A, B, C = new1, new2, new3
print ans
### BUGGY CODE ###

N = int(input())
x, y = (
    zip(*(map(int, input().split()) for _ in range(N))) if N else
    ((), ())
)

class UnionFindTree:
    def __init__(self, n):
        self.p = [i for i in range(n + 1)]
        self.r = [0 for _ in range(n + 1)]

    def find(self, x):
        if self.p[x] != x:
            self.p[x] = self.find(self.p[x])
        return self.p[x]

    def union(self, x, y):
        px = self.find(x)
        py = self.find(y)
        if px != py:
            if self.r[px] < self.r[py]:
                self.p[px] = py
            else:
                self.p[py] = px
                if self.r[px] == self.r[py]:
                    self.r[px] += 1

    def same(self, x, y):
        return self.find(x) == self.find(y)


ans = 10**10

for i in range(N):
    for j in range(N):
        p, q = (x[i] - x[j], y[i] - y[j])
        uft = UnionFindTree(N)
        for u in range(N):
            for v in range(N):
                if (x[u] - x[v], y[u] - y[v]) == (p, q) or (x[u] - x[v], y[u] - y[v]) == (-p, -q):
                    uft.union(u, v)
        ans = min(ans, len({uft.find(z) for z in range(N)}))

print(ans)


### FIXED CODE ###

N = int(input())
x, y = (
    zip(*(map(int, input().split()) for _ in range(N))) if N else
    ((), ())
)

class UnionFindTree:
    def __init__(self, n):
        self.p = [i for i in range(n + 1)]
        self.r = [0 for _ in range(n + 1)]

    def find(self, x):
        if self.p[x] != x:
            self.p[x] = self.find(self.p[x])
        return self.p[x]

    def union(self, x, y):
        px = self.find(x)
        py = self.find(y)
        if px != py:
            if self.r[px] < self.r[py]:
                self.p[px] = py
            else:
                self.p[py] = px
                if self.r[px] == self.r[py]:
                    self.r[px] += 1

    def same(self, x, y):
        return self.find(x) == self.find(y)


ans = 10**10

for i in range(N):
    for j in range(N):
        p, q = (x[i] - x[j], y[i] - y[j])
        uft = UnionFindTree(N)
        for u in range(N - 1):
            for v in range(u + 1, N):
                if (x[u] - x[v], y[u] - y[v]) == (p, q) or (x[u] - x[v], y[u] - y[v]) == (-p, -q):
                    uft.union(u, v)
        ans = min(ans, len({uft.find(z) for z in range(N)}))

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

n,k= list(map(int, input().split()))
INF=998244353
s=[]
for i in range(k):
    j,k=map(int,input().split())
    s+=[l for l in range(j,k+1)]
pattern=[0 for i in range(n)]
pattern[0]=1
ls=len(s)
s.sort()
for i in range(1,n):
    tmp=0
    for j in s:
        if i-j<0:
            break
        tmp+=pattern[i-j]
        tmp%=INF
    pattern[i]=tmp
print(pattern[-1])

### FIXED CODE ###

n,k= list(map(int, input().split()))
INF=998244353
s=[]
for i in range(k):
    j,jj=map(int,input().split())
    s.append([l for l in range(j,jj+1)])
pattern=[0 for i in range(n)]
pattern[0]=1
ls=len(s)
memo=[0 for i in range(k)]
s.sort()
for i in range(1,n):
    tmp=0
    for index,j in enumerate(s):
        if i-j[0]<0:
            break
        elif i-j[-1]>0:
            memo[index]+=pattern[i-j[0]]
            memo[index]-=pattern[i-j[-1]-1]
            if memo[index]<0:
                memo[index]+=INF
        else:
            memo[index]+=pattern[i-j[0]]

        #tmp+=sum(pattern[max(i-j[-1],0):i-j[0]+1])
        memo[index]%=INF
    pattern[i]=(sum(memo)%INF)
print(pattern[-1])
### BUGGY CODE ###

N = int(input())
a = list(map(int, input().split(" ")))

ref = [a.count(i)-1 for i in list(set(a)) if a.count(i)!=1]

ans = N - ((sum(ref)+1)//2)*2

print(ans)

### FIXED CODE ###

from collections import Counter

N = int(input())
a = list(map(int, input().split(" ")))
c = Counter(a)

ref = [v-1 for k, v in c.items() if v != 1]

ans = N - ((sum(ref)+1)//2)*2

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

#import numpy as np
#from numpy import*
#from scipy.sparse.csgraph import shortest_path #shortest_path(csgraph=graph) # dijkstra# floyd_warshall
#from scipy.sparse import csr_matrix
from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby accumulate
from operator import mul,itemgetter
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)
#input=sys.stdin.readline
input=sys.stdin.readline

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

def DIJKSTRA(point,d):
    cost      = [float("inf")]*(n+1)
    Q         = []
    cost[point]=0
    heappush(Q,(point,cost[point]))
    while Q:
        prefnd,tmpC=heappop(Q)
        if cost[prefnd]<tmpC:
            continue
        for node,c in d[prefnd]:
            altC=c+tmpC
            if cost[node]>altC:
                cost[node]=altC
                heappush(Q,(node,altC))
    return cost

def main():
    
    d=[[]for i in range(n+1)]
    
    for i in range(m):
        a,b,c=map(int,input().split())
        d[a].append((b,c))
        d[b].append((a,c))
    for i in range(1,n+1):
        for j in range(i+1,n+1):
            d[j].append((i,0))
#    print(d)
    x=DIJKSTRA(1,d)[n]
    if x!=float("inf"):
        print(x)
    else:
        print(-1)
        
if __name__ == '__main__':
    main()










### FIXED CODE ###

#import numpy as np
#from numpy import*
#from scipy.sparse.csgraph import shortest_path #shortest_path(csgraph=graph) # dijkstra# floyd_warshall
#from scipy.sparse import csr_matrix
from collections import* #defaultdict Counter deque appendleft
from fractions import gcd
from functools import* #reduce
from itertools import* #permutations("AB",repeat=2) combinations("AB",2) product("AB",2) groupby accumulate
from operator import mul,itemgetter
from bisect import* #bisect_left bisect_right
from heapq import* #heapify heappop heappushpop
from math import factorial,pi
from copy import deepcopy
import sys
sys.setrecursionlimit(10**8)
#input=sys.stdin.readline
input=sys.stdin.readline

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

def DIJKSTRA(point,d):
    cost      = [float("inf")]*(n+1)
    Q         = []
    cost[point]=0
    heappush(Q,(point,cost[point]))
    while Q:
        prefnd,tmpC=heappop(Q)
        if cost[prefnd]<tmpC:
            continue
        for node,c in d[prefnd]:
            altC=c+tmpC
            if cost[node]>altC:
                cost[node]=altC
                heappush(Q,(node,altC))
    return cost

def main():
    
    d=[[]for i in range(n+1)]
    
    for i in range(m):
        a,b,c=map(int,input().split())
        d[a].append((b,c))
    for i in range(1,n+1):
        d[i].append((i-1,0))
#    print(d)
    x=DIJKSTRA(1,d)[n]
    if x!=float("inf"):
        print(x)
    else:
        print(-1)
        
if __name__ == '__main__':
    main()
### BUGGY CODE ###

n=int(input())
s=input()
A=[int(i) for i in range(10)]
ans=0
import itertools
for a,b,c in itertools.product(A,repeat=3):
  a,b,c=str(a),str(b),str(c)
  chk=0
  for ss in s:
    if chk==0 and ss==a:
      chk=1
    elif chk==1 and ss==b:
      chk=2
    elif chk==2 and ss==c:
      chk=3
  if chk==3:
    ans+=1
print(ans)

### FIXED CODE ###

n=int(input())
s=input()
inf=float("inf")
A=[[inf]*10 for _ in range(n+1)]
for i in reversed(range(n)):
  ss=s[i]
  for j in range(10):
    if j==int(ss):
      A[i][j]=i
    else:
      A[i][j]=A[i+1][j]
ans=0
for x in range(1000):
  x=str(x).zfill(3)
  cur=0
  for xx in x:
    xx=int(xx)
    cur=A[cur][xx]+1
    if cur==inf:
      break
  if cur!=inf:
    ans+=1
print(ans)
### BUGGY CODE ###

from collections import defaultdict,deque

H, W = map(int, input().split())
S = list(map(int, input().split()))
G = list(map(int, input().split()))
S[0] -= 1
S[1] -= 1
G[0] -= 1
G[1] -= 1
c = [input() for i in range(H)]
dist = [[-1]*W for _ in range(H)]

for i in range(H):
    for g in range(W):
        if c[i][g] == "#":
            dist[i][g] = -2

checkList = set()
que = deque()
que.append(S)
dist[S[0]][S[1]] = 0

directions = [(0, -1), (0, 1), (1, 0), (-1, 0)]

cost = 0

while True:
    if len(que) == 0:
        cost += 1
        for h, w in checkList:
            for i in [-2, -1, 0, 1, 2]:
                for g in [-2, -1, 0, 1, 2]:
                    nh = h+i
                    nw = w+g
                    if nw < 0 or nw > W-1 or nh < 0 or nh > H-1:
                        continue
                    else:
                        if dist[nh][nw] == -1:
                            que.append((nh, nw))
        checkList = set()
        if len(que) == 0:
            print(-1)
            exit()
    else:
        h, w = que.popleft()
        if h == G[0] and w == G[1]:
            print(cost)
            exit()
        checkList.add((h, w))
        for i in range(4):
            nh = h+directions[i][0]
            nw = w+directions[i][1]
            if nw < 0 or nw > W-1 or nh < 0 or nh > H-1:
                continue
            if dist[nh][nw] == -1:
                que.append((nh, nw))
                dist[nh][nw] = cost

### FIXED CODE ###

from collections import defaultdict,deque

H, W = map(int, input().split())
S = list(map(int, input().split()))
G = list(map(int, input().split()))
S[0] -= 1
S[1] -= 1
G[0] -= 1
G[1] -= 1
c = [input() for i in range(H)]
dist = [[-1]*W for _ in range(H)]

for i in range(H):
    for g in range(W):
        if c[i][g] == "#":
            dist[i][g] = -2

checkList = set()
que = deque()
que.append(S)

directions = [(0, -1), (0, 1), (1, 0), (-1, 0)]

cost = 0

while True:
    if len(que) == 0:
        cost += 1
        que = set()
        for h, w in checkList:
            for i in range(-2, 3):
                for g in range(-2, 3):
                    nh = h+i
                    nw = w+g
                    if nw < 0 or nw > W-1 or nh < 0 or nh > H-1:
                        continue
                    else:
                        if dist[nh][nw] == -1:
                            que.add((nh, nw))
        que = deque(que)
        checkList = set()
        if len(que) == 0:
            print(-1)
            exit()
    else:
        h, w = que.popleft()
        dist[h][w] = cost
        if h == G[0] and w == G[1]:
            print(cost)
            exit()
        for i in range(4):
            nh = h+directions[i][0]
            nw = w+directions[i][1]
            if nw < 0 or nw > W-1 or nh < 0 or nh > H-1:
                continue
            if dist[nh][nw] == -1:
                que.append((nh, nw))
                dist[nh][nw] = cost
            elif dist[nh][nw] == -2:
                checkList.add((h, w))
### BUGGY CODE ###

import sys

input=sys.stdin.readline

N,K=map(int,input().split())
integer=[]
for i in range(N):
    a,b=map(int,input().split())
    integer.append([a,b])

bit=[]
for i in range(0,30):
    if K>>i &1==1:
        bit.append(i)

ans=0
for j in range(N):
    test=0
    for j in range(N):
        a,b=integer[j]
        if a|K==K:
            test+=b
    ans=max(ans,test)

for i in bit:
    check=(K>>i)-1
    test=0
    for j in range(N):
        a,b=integer[j]
        if a>>i|check==check:
            test+=b
    ans=max(ans,test)

print(ans)

### FIXED CODE ###

import sys

input=sys.stdin.readline

N,K=map(int,input().split())
integer=[]
for i in range(N):
    a,b=map(int,input().split())
    integer.append([a,b])

bit=[]
for i in range(0,30):
    if K>>i &1==1:
        bit.append(i)

ans=0
for j in range(N):
    a,b=integer[j]
    if a|K==K:
        ans+=b

for i in bit:
    check=(K>>i)-1
    test=0
    for j in range(N):
        a,b=integer[j]
        if a>>i|check==check:
            test+=b
    ans=max(ans,test)

print(ans)