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

n,c = [int(i) for i in input().split()]
d = [] 
for i in range(c):
    d.append(list(map(int, input().split())))
color = []
for i in range(n):
    color.append(list(map(int, input().split())))
for i in range(n):
    for j in range(n):
        color[i][j] -= 1
dofc = [[0 for i in range(3)] for j in range(c)]
for a in range(c):
        for l in range(n):
            for row in range(n):
                dofc[a][(l+row)%3] += d[color[row][l]][a]
D = dofc[0][0]+dofc[1][1]+dofc[2][2]
for c0 in range (c):
    for c1 in range (c):
        if c1 == c0:
            continue
        for c2 in range (c):
            if c2 == c1 or c2 == c0:
                continue
            D = min(D,dofc[c0][0]+dofc[c1][1]+dofc[c2][2])
print(D)

### FIXED CODE ###

n,c = [int(i) for i in input().split()]
d = [[int(i) for i in input().split()]for i in range(c)]
#d[A][B] はAからBになるときの違和感
color = [[int(i) for i in input().split()]for i in range(n)]

devcol = [[0 for i in range(3)] for j in range(c)]
for l in range(n):
        for row in range(n):
            devcol[color[l][row]-1][(l+row)%3] += 1
Dsum = [[0 for i in range(3)] for j in range(c)]
for i in range(c):
    for j in range(c):
        for k in range(3):
            Dsum[i][k] += d[j][i]*devcol[j][k] 
D = Dsum[0][0]+Dsum[1][1]+Dsum[2][2]
for c0 in range (c):
    for c1 in range (c):
        if c1 == c0:
            continue
        for c2 in range (c):
            if c2 == c1 or c2 == c0:
                continue
            D = min(D,Dsum[c0][0]+Dsum[c1][1]+Dsum[c2][2])
#O(c^3)のつもり
print(D)
### BUGGY CODE ###

#!/usr/bin/env python
# coding: utf-8

# In[27]:


import numpy as np
H, W = map(int, input().split())
S = np.array([list(input()) for i in range(H)])
"""
4 6
#..#..
.....#
....#.
#.#...
"""


# In[51]:


row_beam = np.zeros((H, W))
col_beam = np.zeros((H, W))
sh, sw = np.where(S=='#')


# In[52]:


#np.where()
for i in range(H):
    #print(i)
    #print(row_beam)
    row = S[i]
    #sharp_pos = np.where(row=='#')[0]
    sharp_pos = sw[sh==i]
    #print(sharp_pos)
    if len(sharp_pos) == 0:
        row_beam[i] = W
    else:
        s = 0
        for sp in sharp_pos:
            contin = sp-s
            row_beam[i, s:sp] = contin
            s = sp+1
        if s != W:
            row_beam[i, s:W] = W-s
            
for i in range(W):
    #print(i)
    #print(col_beam)
    col = S[:,i]
    #sharp_pos = np.where(col=='#')[0]
    sharp_pos = sh[sw==i]
    #print(sharp_pos)
    if len(sharp_pos) == 0:
        col_beam[:, i] = H
    else:
        s = 0
        for sp in sharp_pos:
            contin = sp-s
            col_beam[s:sp, i] = contin
            s = sp+1
        if s != W:
            col_beam[s:H, i] = H-s


# In[53]:


print(int(np.max(row_beam+col_beam-1)))


# In[54]:


#print(row_beam)
#print(col_beam)


# In[ ]:






### FIXED CODE ###

#!/usr/bin/env python
# coding: utf-8

# In[76]:


import numpy as np
H, W = map(int, input().split())
S = np.array([list(input()) for i in range(H)])
"""
8 8
..#...#.
....#...
##......
..###..#
...#..#.
##....#.
#...#...
###.#..#
"""


# In[84]:


field = np.ones((H, W))
field[S=='#'] = 0
lm, rm, um, dm = (field.copy() for _ in [0]*4)
#print(field)
#print(lm)
#print(rm)
#print(um)
#print(dm)


# In[87]:


#np.where()
for i in range(1, H):
    um[i] = (um[i-1]+1) * field[i]
    dm[-1-i] = (dm[-i]+1) * field[-1-i]
    
for i in range(1, W):
    lm[:, i] = (lm[:, i-1]+1) * field[:, i]
    rm[:, -1-i] = (rm[:, -i]+1) * field[:, -1-i]
    #print(rm)


# In[88]:


#print(field)
#print(um)
#print(dm)
#print(lm)
#print(rm)
print(int(np.max(um+dm+lm+rm)-3))


# In[ ]:
### BUGGY CODE ###

*s, = input()
_q = int(input())
r = 0
for _ in range(_q):
    *q, = map(str, input().split())
    q.extend([" ", " "])
    t = q[0]
    f = q[1]
    c = q[2]

    if t == "1":
        r += 1
    else:
        if r%2 == 0:
            if f == "1":
                s.insert(0, c)
            else:
                s.append(c)
        else:
            if f == "1":
                s.append(c)
            else:
                s.insert(0, c)

if r%2 == 1:
    *s, = reversed(s)

print("".join(s))


### FIXED CODE ###

*s, = input()
_q = int(input())
rev = 0
l, r = [], []
for _ in range(_q):
    *q, = map(str, input().split())

    if q[0] == "1":
        rev += 1
        continue

    t = q[0]
    f = q[1]
    c = q[2]

    if rev%2 == 0:
        if f == "1":
            l.append(c)
        else:
            r.append(c)
    else:
        if f == "1":
            r.append(c)
        else:
            l.append(c)

*l, = reversed(l)

s = l + s + r

if rev%2 == 1:
    *s, = reversed(s)

print("".join(s))
### BUGGY CODE ###

h,n=map(int,input().split())
a,b=[],[]
for i in range(n):
    a_sub,b_sub=map(int,input().split())
    a.append(a_sub)
    b.append(b_sub)
inf=10000000000000
ma=max(a)
dp=[inf]*(h+1+ma)
dp[0]=0
for i in range(n):
    x=[a[i],b[i]]
    for j in range(h+1+ma):
        if dp[j]!=inf:
            if j+x[0]<=h+ma:
                dp[j+x[0]]=min(dp[j+x[0]],dp[j]+x[1])
            else:
                break
print(min(dp[h:]))

### FIXED CODE ###

h,n=map(int,input().split())
a,b=[],[]
for i in range(n):
    a_sub,b_sub=map(int,input().split())
    a.append(a_sub)
    b.append(b_sub)
inf=10000000000000
ma=max(a)
dp=[inf]*(h+1+ma)
dp[0]=0
for i in range(n):
    x=[a[i],b[i]]
    for j in range(h+1+ma):
        if dp[j]!=inf:
            if j+x[0]<=h+ma:
                dp[j+x[0]]=min(dp[j+x[0]],dp[j]+x[1])
            else:
                break
print(min(dp[h:]))
### BUGGY CODE ###

import numpy as np

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

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

dp = np.zeros(K+1)

for k in range(0,K+1):
    for i in A:
        if dp[k] == 0 and k-i >= 0 and dp[k-i] == 0:
            dp[k] = 1

            
if dp[k] == 1:
    print('First')
else:
    print('Second')

### FIXED CODE ###

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

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

dp = [0] * (K+1)

for k in range(0,K+1):
    for i in A:
        if dp[k] == 0 and k-i >= 0 and dp[k-i] == 0:
            dp[k] = 1

            
if dp[k] == 1:
    print('First')
else:
    print('Second')
### BUGGY CODE ###

import math

n, m=map(int, input().split())
t = {}
a = int(math.sqrt(m))

s=0

line = [2, 3] + [i%2*2 + i//2 * 6 + 5 for i in range(a//3)]
while s==0:
    for i in line:
        cnt = 0
        while m%i==0:
            m=m//i
            cnt += 1

        if cnt>0:
            t[i]=cnt

        if m==1:
            s=1
            break

def combi(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r)) 

out=1
for tk in t.keys():
    out *= combi(t[tk]+n-1, n-1)
print(out%(1000000007))


### FIXED CODE ###

import math

n, m=map(int, input().split())
t = {}
a = int(math.sqrt(m))

s=0

line = [2, 3] + [i%2*2 + i//2 * 6 + 5 for i in range(a)]
# line = [2] + list(range(3, m+2, 2))

for i in line:
# for i in range(2, m+2, 2):
#     print(i)
    cnt = 0
    while m%i==0:
        m=m//i
        cnt += 1

    if cnt>0:
        t[i]=cnt

    if m==1:
        break

if m!=1:
    t[m]=1

def combi(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r)) 

out=1
for tk in t.keys():
    out *= combi(t[tk]+n-1, n-1)
#     out = out%(1000000007)
print(out%(1000000007))
### BUGGY CODE ###

a, b, c, d = [int(x) for x in input().split()]
ans = a*c
n_ans = a*c
for i in range(a, b+1):
  for j in range(c, d+1):
    n_ans = i*j
    ans = max(ans, n_ans)
print(ans)

### FIXED CODE ###

a, b, c, d = [int(x) for x in input().split()]
ans = [a*c, a*d, b*c, b*d]
print(max(ans))
### BUGGY CODE ###

s = input()
n = len(s)
s_cu = ["0"] * (n+1)
counter = [0] * 2019

counter[0] += 1
for i in range(1, n+1):
    s_cu[i] = s[n-i] + (s_cu[i-1] if i != 1 else "")
    counter[int(s_cu[i]) % 2019] += 1

ans = 0
for i in counter:
    ans += (i * (i - 1)) // 2

print(ans)

### FIXED CODE ###

s = input()
n = len(s)
s_cu = [0] * (n+1)
counter = [0] * 2019
counter[0] += 1
for i in range(n):
    s_cu[i+1] = (int(s[n-i-1]) * pow(10,i,2019) + s_cu[i]) % 2019
    counter[s_cu[i+1]] += 1

ans = 0
for i in counter:
    ans += (i * (i - 1)) // 2

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

n = int(input())

sum=[]
for i in range(1,n+1):
  divisors=[]
  for j in range(1,i+1):
    if i%j == 0:
      divisors.append(j)
  sum.append(i*len(divisors))

ans=0
for k in range(n):
  ans+=sum[k]

print(ans)

### FIXED CODE ###

n = int(input())

ans = 0
for i in range(1, n + 1):
  for j in range(1, n//i + 1):
    if i * j <= n:
      ans += i * j
    else:
      break
      
print(ans)
### BUGGY CODE ###

def main():
    s = input()
    N = len(s)

    l = []
    sum = 0

    for i in range(N):
        if i == N-1:
            l.append(0)
        l.append(int(s[i:N])%2019)
        
    lset = set(l)
    for key in lset:
        m = l.count(key)
        sum += m*(m-1)//2
    print(sum)

if __name__ == "__main__":
    main()

### FIXED CODE ###

s = input()[::-1] # 入力文字列を逆順でsに格納

counts = [0] * 2019
counts[0] = 1

num, d = 0, 1

for char in s:
    num += int(char) * d
    num %= 2019
    d *= 10
    d %= 2019
    counts[num] += 1

ans = 0
for cnt in counts:
    ans += cnt * (cnt - 1) // 2

print(ans) # 答えの出力
### BUGGY CODE ###

n, m = map(int,input().split())
price = list(map(int, input().split()))
price.sort(reverse=True)

while m != 0:
    price[0] //= 2
    for i in range(1,n):
        if price[0] > price[i]:
            price_buf = price[1:i]
            price[i-1] = price[0]
            price[0:i-1] = price_buf
            break
    m -= 1
print(sum(price))


### FIXED CODE ###

import heapq

n, m = map(int,input().split())
price = list(map(int, input().split()))
price = list(map(lambda x: x*(-1), price))
heapq.heapify(price)
while m != 0:
    maxv = heapq.heappop(price)
    heapq.heappush(price, (maxv+1) // 2)
    m -= 1
print(sum(price) * (-1))
### BUGGY CODE ###

N, M = [int(i) for i in input().split()]
parent = [i for i in range(N+1)]
node_num = [1 for i in range(N+1)]
def find(x):
    if parent[x] == x:
        return x
    else:
        parent[x] = find(parent[x])
        return parent[x]

def find_for(x):
    while parent[x] != x:
        x = parent[x]
    return x

bridge = []
for i in range(M):
    A, B = [int(i) for i in input().split()]
    bridge.append([A, B])

bridge = bridge[::-1]
ans_rev = [N*(N-1)//2]
combi = 0
for i in range(M-1):
    A, B = bridge[i]
    r_A = find_for(A)
    r_B = find_for(B)
    if r_A != r_B:
        parent[r_B] = r_A
        combi += node_num[r_B] * node_num[r_A]
        node_num[r_A] += node_num[r_B]
    ans_rev.append(N*(N-1)//2-combi)

ans = ans_rev[::-1]
print(*ans, sep="\n")


### FIXED CODE ###

import sys
sys.setrecursionlimit(10**6)

N, M = [int(i) for i in input().split()]
parent = [i for i in range(N+1)]
node_num = [1 for i in range(N+1)]
def find(x):
    if parent[x] == x:
        return x
    else:
        parent[x] = find(parent[x])
        return parent[x]

bridge = []
for i in range(M):
    A, B = [int(i) for i in input().split()]
    bridge.append([A, B])

bridge = bridge[::-1]
ans_rev = [N*(N-1)//2]
combi = 0
for i in range(M-1):
    A, B = bridge[i]
    r_A = find(A)
    r_B = find(B)
    if r_A != r_B:
        parent[r_B] = r_A
        combi += node_num[r_B] * node_num[r_A]
        node_num[r_A] += node_num[r_B]
    ans_rev.append(N*(N-1)//2-combi)

ans = ans_rev[::-1]
print(*ans, sep="\n")
### BUGGY CODE ###

import heapq
import sys
def input(): return sys.stdin.readline()
from collections import defaultdict
class UnionFind():
    def __init__(self, num):
        self.par = [-1 for _ in range(num)]
    
    def find(self, x):
        if self.par[x] < 0:
            return x
        else:
            x = self.par[x]
            return self.find(x)
    
    def union(self, x, y):
        rx = self.find(x)
        ry = self.find(y)
        if rx != ry:
            if self.par[rx] < self.par[ry]:
                self.par[ry] = rx
            if self.par[rx] > self.par[ry]:
                self.par[rx] = ry
            else:
                self.par[rx] -= 1
                self.par[ry] = rx
        return
def f(nb, nc):
    if nb != N-1:
        return (nb+1, nc)
    return 0
def g(nb, nc):
    if nc != N-1:
        return (nb, nc+1)
    return 0
def inpl(): return [int(i) for i in input().split()]

N, D = inpl()
A = inpl()
B = [(i*D + v, i) for i, v in enumerate(A, 1)]
C = [(-i*D + v, i) for i, v in enumerate(A, 1)]
B.sort()
C.sort()
Z = UnionFind(N+1)
Q = []
ctr = 0
ans = 0
H = defaultdict(lambda: False)
heapq.heappush(Q, (C[0][0] + B[0][0], (0, 0)))
H[(0, 0)] = True
H[0] = True
while ctr < N-1:
    v, (bi, ci) = heapq.heappop(Q)
    _, bj = B[bi]
    _, cj = C[ci]
    if not H[f(bi, ci)]:
        H[(bi+1, ci)] = True
        heapq.heappush(Q, (B[bi+1][0] + C[ci][0], (bi+1, ci)))
    if not H[g(bi, ci)]:
        H[(bi, ci+1)] = True
        heapq.heappush(Q, (B[bi][0] + C[ci+1][0], (bi, ci+1)))
    if bj <= cj:
        continue
    if Z.find(bj) == Z.find(cj):
        continue
    Z.union(bj, cj)
    ans += v
    ctr += 1
print(ans)

### FIXED CODE ###

from operator import itemgetter
import itertools
def inpl(): return list(map(int, input().split()))
def cost(x, y):
    if x == y:
        return float('inf')
    return D * abs(x - y) + A[x] + A[y]
N, D = inpl()
A = inpl()
Right = [(v - i*D, i) for i, v in enumerate(A)]
Left = [(v + i*D, i) for i, v in enumerate(A)][::-1]
Right = list(map(itemgetter(1), itertools.accumulate(Right, min)))
Left = list(map(itemgetter(1), itertools.accumulate(Left, min)))[::-1]
ans = 0
hub = []
for i, (l, r) in enumerate(zip(Left, Right)) :
    if i == l == r:
        hub.append(i)
    else:
        ans += min(cost(i, r), cost(i, l))
print(ans + sum([cost(i, j) for i, j in zip(hub, hub[1:])]))
### BUGGY CODE ###

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod


x,y = map(int,input().split())

mod = 10**9+7

division = (x+y)//3

if (x+y)%3!=0:
    print(0)
    exit()


mod = 10**9+7

#combを求める前処理 O(log division)
g1 = [1, 1] #元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, division + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( pow(i,mod-2,mod) )
    g2.append( (g2[-1] * inverse[-1]) % mod )

r = min(x,y)-division
ans = cmb(division,r,mod)

print(ans)

### FIXED CODE ###

x,y = map(int,input().split())

mod = 10**9+7

division = (x+y)//3

if [x%3,y%3] in [[1,1],[2,2]]:
    print(0)
    exit()

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod

mod = 10**9+7 #出力の制限
N = 10**6
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル
inverse = [0, 1] #逆元テーブル計算用テーブル

for i in range( 2, N + 1 ):
    g1.append( ( g1[-1] * i ) % mod )
    inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
    g2.append( (g2[-1] * inverse[-1]) % mod )



r = min(x,y)-division
ans = cmb(division,r,mod)

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

def dp_fn():
  N, W = map(int, input().split())
  WV = (map(int, input().split()) for _ in range(N))
  
  DP = {0:0}
  
  for w, v in WV:
    keys = sorted(DP.keys(), reverse=True)
    for i in keys:
      if i + w <= W:
        if i+w in DP:
          DP[i+w] = max(DP[i] + v, DP[i+w])
        else:
          DP[i+w] = DP[i] + v
  return max(DP.values())
  
print(dp_fn())


### FIXED CODE ###

import sys
input = sys.stdin.readline

def dp_fn():
  N, W = map(int, input().split())
  WV = (map(int, input().split()) for _ in range(N))
  
  DP = {0:0}
  
  for w, v in WV:
    keys = sorted(DP.keys(), reverse=True)
    for i in keys:
      j = DP[i]
      if j+w <= W:
        if i+v in DP:
          DP[i+v] = min(DP[i+v], j+w)
        else:
          DP[i+v] = j+w
  return max(DP.keys())
  
print(dp_fn())
### BUGGY CODE ###

S = input()
cnt = 0
is_end = [False] * (len(S) + 1)
for i in range(len(S)):
    if is_end[i]:
        continue
    is_end[i] = True
    bingo = [i]
    r = 0
    for j in range(i+1, len(S)+1):
        r = r * 10 + int(S[j-1])
        if is_end[j]:
            continue
        r %= 2019
        if r == 0:
            is_end[j] = True
            bingo.append(j)
    l = len(bingo)
    if l >= 2:
        cnt += int(l*(l-1)/2)
print(cnt)


### FIXED CODE ###

S = input()
mod_bucket = [0] * 2019
mod_bucket[0] += 1

_S = S[::-1]
mod = 0
p = 1
for i in range(len(S)):
    mod = (p * int(_S[i]) + mod) % 2019
    mod_bucket[mod] += 1
    p *= 10
    p %= 2019

ans = sum([mod*(mod-1)/2 for mod in mod_bucket])
print(int(ans))
### BUGGY CODE ###

from sys import exit
import sys
sys.setrecursionlimit(10000)

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 = [1]

for s in range(1, N + 1):
    if s in a:
        step.append(0)
        continue
    if s == 1:
        step.append(1)
        continue
    else:
        step.append(step[s - 1] + step[s - 2])

print(step[N] % 1000000007)


### 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 ###

x,y=map(int,input().split())

if x==y:
  print(-1)
elif y==1:
  print(-1)
else:
  for i in range(1,10**10):
    if x*i%y !=0:
      print(x*i)
      break


### FIXED CODE ###

x,y=map(int,input().split())

if x%y==0:
  print(-1)
else:
  print(x)
### BUGGY CODE ###

n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(m)]
ikeruheya = [[] for i in range(n)]
ans = [0]*(n+1)

ans[0]=-1
ans[1]=1
for i in range(m):
    ikeruheya[a[i][0]-1].append(a[i][1])
    ikeruheya[a[i][1]-1].append(a[i][0])

    
next=[1]
count = 1
while count < n and len(next)!=0:
    
    imaheya = next.pop(0)
    j = 0
    while j < len(ikeruheya[imaheya-1]) and count < n:
    
        if ans[ikeruheya[imaheya-1][j]]==0:
            ans[ikeruheya[imaheya-1][j]]= imaheya             
            next.append(ikeruheya[imaheya-1][j])
            count += 1
            j+=1
        else:
            j+=1

print("Yes")
for i in range(2, n+1):
    print(ans[i])



    
    


### FIXED CODE ###

from collections import deque
n,m = map(int,input().split())
a=[list(map(int, input().split())) for i in range(m)]
b=[[] for j in range(n)] 

michi=[-1]*(n)
michi[0]=0
heya=[]


for i in range(m):
    b[a[i][0]-1].append(a[i][1])
    b[a[i][1]-1].append(a[i][0])



heya.append(1)


while len(heya):
    now=heya.pop(0)-1
    
    for i in range(len(b[now])):
        if michi[b[now][i]-1] == -1:
            heya.append(b[now][i])
            michi[b[now][i]-1] = now+1
print("Yes")
for i in range(1,n):
    print(michi[i])
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
cnt = 0
for i in range(N):
    for j in range(i+1, N+1):
        if sum(A[i:j]) == 0:
            cnt += 1
print(cnt)

### FIXED CODE ###

from math import factorial as f
L = [0]
N = int(input())
A = list(map(int, input().split()))
cnt = 0
dic = {}
dic[0] = 1
for i in range(N):
    n = L[-1] + A[i]
    L.append(n)
    dic.setdefault(n, 0)
    dic[n] += 1
for key in dic.keys():
    if dic[key] > 1:
        cnt += f(dic[key]) // f(2) // f(dic[key] - 2)
print(int(cnt))
### BUGGY CODE ###

def binary_search(a,b,j):
   left=j
   right=N
   while left<right-1:
      mid=(left+right)//2
      if L[mid]<a+b:
         left=mid
      else:
         right=mid

   return left

N=int(input())
L=list(map(int,input().split()))
L.sort()

count=0

for i in range(N):
   for j in range(N):
      if j<=i:
         continue
      if j==N-1:
         continue
      a=L[i]
      b=L[j]

      c_index=binary_search(a,b,j)
      count+=c_index-j

print(count)

### FIXED CODE ###

def binary_search(a,b,j):
   left=j
   right=N
   while left<right-1:
      mid=(left+right)//2
      if L[mid]<a+b:
         left=mid
      else:
         right=mid

   return left

N=int(input())
L=list(map(int,input().split()))
L.sort()

count=0

for i in range(N):
   for j in range(N):
      if j<=i:
         continue
      if j==N-1:
         continue
      a=L[i]
      b=L[j]

      c_index=binary_search(a,b,j)
      count+=c_index-j

print(count)
### BUGGY CODE ###

s = input()
q = int(input())
r = []
#先頭の数
t = []
#2つめ
u = []
#3つめ
v = []
for i in range(q):
    a = input()
    r.append(a)
    t.append(int(r[i][0]))
    if r[i][0] == '1':
        u.append('0')
        v.append('0')
    else:
        u.append(r[i][2])
        v.append(r[i][4])
#
if t.count(1) %2 == 1:
    s = ''.join(list(reversed(s)))


for i,j in enumerate(t):
    if j == 2:
        w = (t[i:]).count(1)
        if w % 2 == 1:
            if u[i] == '1':
                u[i] = '2'
            else:
                u[i] = '1'
        if u[i] == '1':
            s = v[i] + s
        else:
            s = s + v[i]
print(s)




### FIXED CODE ###

from collections import deque

s = input()
q = int(input())
query = [input() for _ in range(q)]

s = deque(s)

inv = 0
for q in query :
    if q[0] == '1':
        inv += 1
    else:
        _,f,c = q.split()
        if f == '1':
            if inv % 2 == 0:
                s.appendleft(c)
            else:
                s.append(c)
        else:
            if inv % 2 ==0:
                s.append(c)
            else:
                s.appendleft(c)
s = ''.join(s)
print(s if inv % 2 == 0 else s[::-1])
### BUGGY CODE ###

n , m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
bc = []
for i in range(m):
    b, c = map(int, input().split())
    bc.append([b,c])
bc.sort(key = lambda x : -x[1])
box = []
for b, c in bc:
    box.extend([c]*b)

for i in range(min(len(box), n)):
    a[i] = max(a[i], box[i])

print(sum(a))


### FIXED CODE ###

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

bc = []
for i in range(m):
    b, c = map(int, input().split())
    bc.append([b,c])

from operator import itemgetter
bc.sort(key=itemgetter(1), reverse = True)
k = 0
c = bc[0][1]
b = bc[0][0]
bn = 0
while A[k] < c:
    A[k]=c
    k+=1
    if k == len(A):
        break
    b-=1
    if b  == 0:
        bn += 1
        if bn == len(bc):
            break
        b = bc[bn][0]
        c = bc[bn][1]
print(sum(A))
### BUGGY CODE ###

n,m=map(int,input().split())
d={i:[] for i in range(n)}
for i in range(m):
    x,y,z=map(int,input().split())
    d[x-1].append(y-1)
    d[y-1].append(x-1)
cnt=0
c=set()
for i in range(n):
    if i in c:
        continue
    c.add(i)
    q=[i]
    while(q):
        p=q.pop(0)
        l=d[p]
        for x in l:
            if x in c:
                continue
            q.append(x)
            c.add(x)
    cnt+=1
print(cnt)

### FIXED CODE ###

def connected_components(graph):
    seen = set()
    def component(n):
        nodes = set([n])
        while nodes:
            n = nodes.pop()
            seen.add(n)
            nodes |= set(graph[n]) - seen
            yield n
    for n in graph:
        if n not in seen:
            yield component(n)

def print_gen(gen):
    print(len([list(x) for x in gen]))

n,m=map(int,input().split())
d={i:[] for i in range(n)}
for i in range(m):
    x,y,z=map(int,input().split())
    d[x-1].append(y-1)
    d[y-1].append(x-1)

print_gen(connected_components(d))
### BUGGY CODE ###

N, P = map(int, input().split())
d = list(map(int, input()))
ans =0
for k in range(0,N):
    for l in range(k+1,N+1):
        num = int("".join(map(str,d[k:l])))
        
        if num % P == 0:
            ans += 1

print(ans)


### FIXED CODE ###

N, P = map(int, input().split())
S = list(map(int, input()))
ans = 0
if P == 2:
    for i in range(N):
        if S[i]%2 == 0:
            ans +=i+1
    print(ans)

elif P ==5:
    for i in range(N):
        if S[i]%5 == 0:
            ans +=i+1
    print(ans)
else:
    data =[0]*P
    tens = 1
    k = 0
    for i in range(N-1,-1,-1):
        k += S[i] *tens %P
        k = k%P
        data[k] += 1
        tens *=10
        tens %=P

    for i in data:
        ans += i*(i-1)/2
    ans +=data[0]
    print(int(ans))