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

import numpy as np
from numba import jit

@jit
def solve(s):
    n = len(s)
    ans = 0
    now = 0
    mp = np.zeros(2019, np.int)
    mp[0] += 1
    tmp = np.zeros(2019, np.int)

    id = np.zeros(2019, np.int)
    for i in range(2019):
        id[i] = i * 10 % 2019

    for c in s:
        now *= 10;
        now += ord(c) - ord("0")
        now %= 2019

        ans += mp[now]
        mp[now] += 1
        for i in range(2019):
            tmp[id[i]] = mp[i]
        for i in range(2019):
            mp[i] = tmp[i]

    print(ans)

s = input()
solve(s)


### FIXED CODE ###

s = input()
n = len(s)
ans = 0
now = 0
mp = [0 for i in range(2019)]
mp[0] += 1
tmp = [0 for i in range(2019)]

id = [0 for i in range(2019)]
for i in range(2019):
    id[i] = i * 10 % 2019

for c in s:
    now *= 10;
    now += ord(c) - ord("0")
    now %= 2019

    ans += mp[now]
    mp[now] += 1
    for i in range(2019):
        tmp[id[i]] = mp[i]
    for i in range(2019):
        mp[i] = tmp[i]

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

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
 
    if temp!=1:
        arr.append([temp, 1])
 
    if arr==[]:
        arr.append([n, 1])
 
    return arr
 
import sys     
N = int(input())
if N == 1:
    print(0)
    sys.exit()
# if isPrime(N)==True:
#     print(0)
fact = factorization(N)
fact_num = []
fact_num2 = []
 
    
for i in range(len(fact)):
    fact_num.append(fact[i][0])
    fact_num2.append(fact[i][1])
 
cnt=0
 
for j in range(10**12):
    for i in range(len(fact)):
        if N%fact_num[i]==0 and N%(fact_num[i]*(j+1))==0:
            N = N/(fact_num[i]*(j+1))
            cnt+=1
 
print(cnt)         
 

### FIXED CODE ###

def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
 
    if temp!=1:
        arr.append([temp, 1])
 
    if arr==[]:
        arr.append([n, 1])
 
    return arr
 
import sys     
N = int(input())
if N == 1:
    print(0)
    sys.exit()

fact = factorization(N)
fact_num = []
fact_num2 = []
 

for i in range(len(fact)):
    fact_num.append(fact[i][0])
    fact_num2.append(fact[i][1])

cnt=0

for j in range(max(fact_num2)+1):
    for i in range(len(fact)):
        if N%fact_num[i]==0 and N%(fact_num[i]**(j+1))==0:
            N = N/(fact_num[i]**(j+1))
            cnt+=1

            if N==1:
                print(cnt)
                sys.exit()
print(cnt)
### BUGGY CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
result=[1]
temp=1
i=0
while i<k:
  temp=a[temp-1]
  if temp in result:
    break
  result.append(temp)
  i=i+1
result.append(temp)
temp=0
l=len(result)
for i in range(0,l):
  if result[l-1]==result[i]:
    temp=i
    break
if k<l:
  print(result[k+1])
else:
  l2=l-1-temp
  print(result[temp+(k-temp)%l2])

### FIXED CODE ###

n,k=map(int,input().split())
a=list(map(int,input().split()))
result=[1]
temp=1
for i in range(n):
  temp=a[temp-1]
  result.append(temp)
temp=result.index(result[len(result)-1])
c=len(result)-temp-1
if k<n:
  print(result[k])
else:
  print(result[temp+(k-temp)%c])
### BUGGY CODE ###

N = int(input())
P = list(map(int, input().split()))

ans = 1
for i in range(1,N):
  if min(P[0:i]) >= P[i]:
    ans += 1

print(ans)

### FIXED CODE ###

N = int(input())
P = list(map(int, input().split()))

ans = 1
tempmin = P[0]
for i in range(1,N):
  if tempmin >= P[i]:
    tempmin = P[i]
    ans += 1

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

r,g,b,n = map(int,input().split())
ans = 0
for i in range(n+1):  
  for j in range(n+1):
    if r*i + g*j > n:
      break
    elif (n - r*i - g*j) % b == 0:
      ans += 1
print(ans)

### FIXED CODE ###

r,g,b,n = map(int,input().split())
a = [0]*(n+1)
a[0] = 1
for i in [r,g,b]:
    for j in range(n+1-i):
        a[j+i]+=a[j]
print(a[n])
### BUGGY CODE ###


def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n=n0()
ab=t3(n-1)

d={i:[] for i in range(1,n+1)}
for a,b in ab:
    d[a].append(b)
    d[b].append(a)

l=list(d.items())
# l.sort(key=lambda x:-len(x[1]))
# m=len(l[0][1])
m=0
d2={line:0 for line in ab}
c={i:[] for i in range(1,n+1)}
for a1,list1 in l:
    m=max(m,len(list1))
    color=1
    for b1 in list1:
        if a1>b1:
            a,b=b1,a1
        else:
            a,b=a1,b1
#         print(a,b)
        if d2[(a,b)]!=0:
            continue
        else:
            while color in c[a] or color in c[b]:
                color+=1 
            d2[(a,b)]=color
            c[a].append(color)
            c[b].append(color)
            color+=1
#     print(d2)
print(m)
ans=[]
for line in ab:
    ans.append(d2[line])
print(*ans,sep="\n")

### FIXED CODE ###


def s0():return input()
def s1():return input().split()
def s2(n):return [input() for x in range(n)]
def s3(n):return [input().split() for _ in range(n)]
def s4(n):return [[x for x in s] for s in s2(n)]
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]
def t3(n):return [tuple(int(x) for x in input().split()) for _ in range(n)]
def p0(b,yes="Yes",no="No"): print(yes if b else no)
# from sys import setrecursionlimit
# setrecursionlimit(1000000)
# from collections import Counter,deque,defaultdict
# import itertools
# import math
# import networkx as nx
# from bisect import bisect_left,bisect_right
# from heapq import heapify,heappush,heappop
n=n0()
ab=t3(n-1)

d={i:[] for i in range(1,n+1)}
for a,b in ab:
    d[a].append(b)
    d[b].append(a)

l=list(d.items())
l.sort(key=lambda x:-len(x[1]))
m=len(l[0][1])

d2={line:0 for line in ab}
c={i:[] for i in range(1,n+1)}
for node,list1 in l:
    a=node
    color=1
#     print(node,list1)
    for b1 in list1:
        if node>b1:
            a,b=b1,node
        else:
            a,b=node,b1
#         print(a,b)
        if d2[(a,b)]!=0:
            continue
        else:
            while color in c[a] or color in c[b]:
                color+=1 
            d2[(a,b)]=color
            c[a].append(color)
            c[b].append(color)
            color+=1
#     print(d2)
print(m)
for line in ab:
    print(d2[line])
### BUGGY CODE ###

s = input()
k = int(input())
n = len(s)

s = list(s)
ans = []
for i in range(n + 1):
    for j in range(i + 1, n + 1):
        ans.append(str(''.join(s[i:j])))

ans = sorted(list(set(ans)))
print(ans[k - 1])


### FIXED CODE ###

s = input()
k = int(input())
n = len(s)

s = list(s)
ans = []
for i in range(n + 1):
    for j in range(i + 1, min(i + 1 + k, n + 1)):
        ans.append(str(''.join(s[i:j])))

ans = sorted(list(set(ans)))
print(ans[k - 1])
### BUGGY CODE ###

from collections import Counter

N = int(input())
C = tuple(map(lambda x: int(x)-1,input().split()))

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


parent = [None]*N
parent[0] = -1

degree = [0]*N
stack = [0]
while stack:
    p = stack.pop()
    for c in adj[p]:
        if parent[c] is None:
            parent[c] = p
            degree[p] += 1
            stack.append(c)

leaves = [i for i in range(N) if degree[i] == 0]

dp = [Counter() for _ in range(N)] # color-wise counter
subtreesize = [0]*N
cnt = [0]*N

while leaves:
    c = leaves.pop()
    p = parent[c]


    if p >= 0:
        dp[c][C[c]] = subtreesize[c]+1
        x = subtreesize[c]-dp[c][C[p]]+1
        cnt[C[p]] += x*(x+1)//2
        subtreesize[p] += subtreesize[c]+1

        a,b = dp[p],dp[c]
        if len(a) > len(b):
            a += b
            dp[p] = a
        else:
            b += a
            dp[p] = b
        dp[c] = None
        degree[p] -= 1
        if degree[p] == 0:
            leaves.append(p)

dp[0][C[0]] = subtreesize[0]+1
for c in range(N):
    x = subtreesize[0]-dp[0][c]+1
    cnt[c] += x*(x+1)//2

total = (subtreesize[0]+1)*(subtreesize[0]+2)//2

print(*(total-v for v in cnt), sep='\n')


### FIXED CODE ###

from collections import defaultdict
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

N = int(readline())
C = tuple(map(lambda x: int(x)-1, readline().split()))

temp = map(int,read().split())
temp = zip(temp,temp)

adj = [list() for _ in range(N)]
for a,b in temp:
    adj[a-1].append(b-1)
    adj[b-1].append(a-1)


parent = [None]*N
parent[0] = -1

stack = [0]
order = []
while stack:
    p = stack.pop()
    order.append(p)
    for c in adj[p]:
        if parent[c] is None:
            parent[c] = p
            stack.append(c)

def merge(a,b):
    if len(a) > len(b):
        a,b = b,a
    for k,v in a.items():
        b[k] += v
    return b

dp = [defaultdict(int) for _ in range(N)] # color-wise counter
subtreesize = [0]*N
cnt = [N*(N+1)//2]*N

for c in reversed(order):
    p = parent[c]
    if p >= 0:
        dp[c][C[c]] = subtreesize[c]+1
        x = subtreesize[c]-dp[c][C[p]]+1
        cnt[C[p]] -= x*(x+1)//2
        subtreesize[p] += subtreesize[c]+1

        dp[p] = merge(dp[c],dp[p])
        dp[c] = None

dp[0][C[0]] = subtreesize[0]+1
for c in range(N):
    x = subtreesize[0]-dp[0][c]+1
    cnt[c] -= x*(x+1)//2

print(*cnt, sep='\n')
### BUGGY CODE ###

#13:19
n = int(input())
s = input()
t = []
for i in range(n):
  t.append(ord(s[i])-97)
q = int(input())
scale = 19
seg = [[0 for _ in range(2**(scale+1))] for _ in range(26)]
for i in range(n):
  tmp = int(i) + 2 ** scale
  x = t[i]
  while tmp != 0:
    seg[x][tmp] += 1
    tmp //= 2
#print(seg)
raw = []
for _ in range(q):
  d,a,b = input().split()
  d = int(d)
  if d == 1:
    a = int(a) - 1
    b = ord(b) - 97
  else:
    a = int(a) - 2
    b = int(b) - 1
  raw.append([d,a,b])
for p in range(q):
  d,a,b = raw[p]
  if d == 1:
    tmp = int(a)
    c = t[tmp]
    tmp += 2**scale
    while tmp != 0:
      seg[c][tmp] -= 1
      tmp >>= 1
    tmp = int(a)
    t[tmp] = b
    tmp += 2**scale
    while tmp != 0:
      seg[b][tmp] += 1
      tmp >>= 1
  else:
    ans = 0
    for d in range(26):
      l = 0
      r = 0
      if a == -1:
        l = 0
      else:
        tmp = a + 2**scale
        l += seg[d][tmp]
        while tmp != 1:
          if tmp % 2 == 1:
            l += seg[d][tmp-1]
          tmp >>= 1
      tmp = b + 2**scale
      r += seg[d][tmp]
      while tmp != 1:
        if tmp % 2 == 1:
          r += seg[d][tmp-1]
        tmp >>= 1
      if r > l:
        ans += 1
      #print(d,r,l)
    print(ans)
#print(t)

### FIXED CODE ###

#13:19
n = int(input())
s = input()
t = []
for i in range(n):
  t.append(ord(s[i])-97)
q = int(input())
scale = 19
seg = [[0 for _ in range(2**(scale+1))] for _ in range(26)]
for i in range(n):
  tmp = int(i) + 2 ** scale
  x = t[i]
  while tmp != 0:
    seg[x][tmp] += 1
    tmp //= 2
#print(seg)
raw = []
for _ in range(q):
  d,a,b = input().split()
  d = int(d)
  if d == 1:
    a = int(a) - 1
    b = ord(b) - 97
  else:
    a = int(a) - 2
    b = int(b) - 1
  raw.append([d,a,b])
for p in range(q):
  d,a,b = raw[p]
  if d == 1:
    tmp = int(a)
    c = t[tmp]
    tmp += 2**scale
    while tmp != 0:
      seg[c][tmp] -= 1
      tmp >>= 1
    tmp = int(a)
    t[tmp] = b
    tmp += 2**scale
    while tmp != 0:
      seg[b][tmp] += 1
      tmp >>= 1
  else:
    ans = 0
    for d in range(26):
      l = 0
      r = 0
      if a == -1:
        l = 0
      else:
        tmp = a + 2**scale
        l += seg[d][tmp]
        while tmp != 1:
          if tmp & 1:
            l += seg[d][tmp-1]
          tmp >>= 1
      tmp = b + 2**scale
      r += seg[d][tmp]
      while tmp != 1:
        if tmp & 1:
          r += seg[d][tmp-1]
        tmp >>= 1
      if r > l:
        ans += 1
      #print(d,r,l)
    print(ans)
#print(t)
### BUGGY CODE ###

import heapq

N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]

ans = 0
for i in range(N):
    for j in range(N):
        via_k = float('inf')
        for k in range(N):
            if A[i][j] > A[i][k] + A[k][j]:
                print(-1)
                exit()
            if k == i or k == j:
                continue
            via_k = min(via_k, A[i][k] + A[k][j])
        if i <= j:
            continue
        if A[i][j] < via_k:
            ans += A[i][j]
print(ans)


### FIXED CODE ###

import heapq

N = int(input())
A = [list(map(int, input().split())) for _ in range(N)]

ans = 0
for i in range(N):
    for j in range(N):
        via_k = float('inf')
        for k in range(N):
            if A[i][j] > A[i][k] + A[k][j]:
                print(-1)
                exit()
            if k == i or k == j:
                continue
            via_k = min(via_k, A[i][k] + A[k][j])
        if i <= j:
            continue
        if A[i][j] < via_k:
            ans += A[i][j]
print(ans)
### BUGGY CODE ###

x = input()
N = int(x)
A = list(map(int,input().split()))
a = 0

for i in range(N-1):
    for j in range(N-i-1):
        a = a + A[i]*A[i+j+1]
print(a % (10**9+7))

### FIXED CODE ###

x = input()
N = int(x)
A = list(map(int,input().split()))
a = 0
b = 0

for i in range(N-1):
    a = a + A[i+1]

for i in range(N-1):
    b = b + A[i]*a
    a = a - A[i+1]

print(b % (10**9+7))
### BUGGY CODE ###

while True:
    try:
        N = int(input())
        if N>0 or N<=100:
            A = []
            for i in range(0,N):
              B = int(input())
              if B>0 or B<=100:
                  A.append(B)

            sum1 = float(1/A[0])

            for i in range(0,N-1):
              sum1 = sum1 + 1/A[i+1]

            result = 1/sum1
            print(result)

            break

    except:
            print("Error")

### FIXED CODE ###

try:
    N = int(input())
    if N>0 or N<=100:
        S = input()
        A = S.split()
        A = list(map(int, A)) 
 
        sum1 = 1/A[0]
 
        for i in range(0,N-1):
         sum1 = sum1 + 1/A[i+1]
 
        result = 1/sum1
        print(result)

except:
        print("Error")
### BUGGY CODE ###

N = int(input())
d = [list(map(int, input().split())) for _ in range(N)]

check = [[1] * N for _ in range(N)]

for i in range(N):
    for j in range(i + 1, N):
        if i == j:
            continue
        for k in range(N):
            if i == k or j == k:
                pass
            else:                    
                if d[i][j] > d[i][k] + d[k][j]:
                    print (-1)
                    exit()
                elif d[i][j] == d[i][k] + d[k][j]:
                    check[i][j] = 0

ans = 0
for i in range(N):
    for j in range(i + 1, N):
        ans += d[i][j] * check[i][j]

print (ans)

### FIXED CODE ###

N = int(input())
d = [list(map(int, input().split())) for _ in range(N)]

check = [[1] * N for _ in range(N)]

for i in range(N):
    for j in range(N):
        if i == j:
            continue
        for k in range(N):
            if i == k or j == k:
                pass
            else:                    
                if d[i][j] > d[i][k] + d[k][j]:
                    print (-1)
                    exit()
                elif d[i][j] == d[i][k] + d[k][j]:
                    check[i][j] = 0

ans = 0
for i in range(N):
    for j in range(N):
        ans += d[i][j] * check[i][j]

print (ans // 2)
### BUGGY CODE ###

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


### FIXED CODE ###

import collections
a = int(input())
l = list(map(int, input().split()))
l_2 = collections.Counter(l)
for i in range(a):
    print(l_2[i + 1])
### BUGGY CODE ###

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = [[c for c in S()] for _ in range(n)]
    r = 0
    for i in range(n):
        b = [[a[ii][(j+i)%n] for j in range(n)] for ii in range(n)]
        f = True
        for ii in range(n):
            for jj in range(ii+1,n):
                if b[ii][jj] != b[jj][ii]:
                    f = False
                    break
            if not f:
                break
        if f:
            r += 1

    return r * n




print(main())



### FIXED CODE ###

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    n = I()
    a = [[c for c in S()] for _ in range(n)]
    r = 0
    for i in range(n):
        b = [[a[ii][(j+i)%n] for j in range(n)] for ii in range(n)]
        f = True
        for ii in range(n):
            for jj in range(ii+1,n):
                if b[ii][jj] != b[jj][ii]:
                    f = False
                    break
            if not f:
                break
        if f:
            r += 1

    return r * n




print(main())
### BUGGY CODE ###

N = int(input())
A = list(input().split())
while A!=[]:
    a = A.pop()
    if A.count(a) > 0:
        print('NO')
        exit()
print('YES')

### FIXED CODE ###

N = int(input())
A = list(input().split())
A.sort()
for i in range(N-1):
    if A[i]==A[i+1]:
        print('NO')
        exit()
print('YES')
### BUGGY CODE ###

s = input()
q = int(input())
que = [0] * q
t = []
f = []
c = []
for i in range(q):
    que[i] = list(map(str,input().split()))
    if(len(que[i])==3):
        t.append(que[i][0])
        f.append(que[i][1])
        c.append(que[i][2])
    else:
        t.append(que[i][0])
count = 0
for i in range(q):
    if t[i] == '1':
        s = s[::-1]
    else:
        if f[count] == '1':
            s = c[count] + s
            count += 1
        else:
            s += c[count]
            count += 1
print(s)

### FIXED CODE ###

from collections import deque
s = input()
sque = deque(s)
q = int(input())
que = [0] * q
flg = False
for i in range(q):
    que[i] = (list(map(str,input().split())))
    if(len(que[i])==3):
        if que[i][1] == '1':
            if flg:
                sque.append(que[i][2])
            else:
                sque.appendleft(que[i][2])
        else:
            if flg:
                sque.appendleft(que[i][2])
            else:
                sque.append(que[i][2])
    else:
        if flg:
            flg = False
        else:
            flg = True
if flg:
    sque.reverse()
    print("".join(list(sque)))
else:
    print("".join(list(sque)))
### BUGGY CODE ###

N = int(input())

A = [None] * N
B = [None] * N

for i in range(N):
    a, b = map(int, input().split())
    A[i] = a
    B[i] = b

medians = []

if N % 2 == 0:
    a_median = (A[N // 2 - 1] + A[N // 2]) // 2
    b_median = (B[N // 2 - 1] + B[N // 2]) // 2
    i = a_median
    while i <= b_median:
        medians.append(i)
        i += 0.5
else:
    a_median = A[(N + 1) // 2 - 1]
    b_median = B[(N + 1) // 2 - 1]
    for i in range(a_median, b_median + 1):
        medians.append(i)

print(len(medians))


### FIXED CODE ###

from statistics import median

n = int(input())
a = [None] * n
b = [None] * n
for i in range(n):
    a[i], b[i] = map(int, input().split())

if n % 2 == 1:
    print(abs(median(b) - median(a)) + 1)
else:
    print(int(abs(median(b) - median(a)) * 2) + 1)
### BUGGY CODE ###

from sys import setrecursionlimit
import numpy as np


setrecursionlimit(10**6)

N, M = tuple(map(int, input().split()))
Ls = np.zeros((M,), dtype=int)
Rs = np.zeros((M,), dtype=int)
Ds = np.zeros((M,), dtype=int)

for i in range(M):
    Ls[i], Rs[i], Ds[i] = tuple(map(int, input().split()))

Ls = Ls - 1
Rs = Rs - 1

adjacency_list = []
for i in range(N):
    adjacency_list.append([])

for i in range(M):
    adjacency_list[Ls[i]].append((Rs[i], Ds[i]))
    adjacency_list[Rs[i]].append((Ls[i], -Ds[i]))

visited = np.zeros(N, dtype=bool)
weights = np.zeros(N, dtype=int)
to_be_visited = []


def DFS(node_index):
    global adjacency_list, visited, weights, to_be_visited
    adjacent_node_information = adjacency_list[node_index]
    for adjacent_node_index, adjacent_edge_weight in adjacent_node_information:
        if not visited[adjacent_node_index]:
            visited[adjacent_node_index] = True
            to_be_visited.append(adjacent_node_index)
            weights[adjacent_node_index] = weights[node_index] + adjacent_edge_weight

    if len(to_be_visited) == 0:
        return 0
    else:
        DFS(to_be_visited.pop())

pointer = 0
for pointer in range(N):
    if not visited[pointer]:
        to_be_visited.append(pointer)
        DFS(pointer)

for i in range(M):
    if weights[Rs[i]] - weights[Ls[i]] != Ds[i]:
        print("No")
        exit()

print("Yes")

### FIXED CODE ###

import numpy as np

N, M = tuple(map(int, input().split()))
Ls = np.zeros((M,), dtype=int)
Rs = np.zeros((M,), dtype=int)
Ds = np.zeros((M,), dtype=int)

for i in range(M):
    Ls[i], Rs[i], Ds[i] = tuple(map(int, input().split()))

# debug 1 exit()

Ls = Ls - 1
Rs = Rs - 1

adjacency_list = []
for i in range(N):
    adjacency_list.append([])

for i in range(M):
    adjacency_list[Ls[i]].append((Rs[i], Ds[i]))
    adjacency_list[Rs[i]].append((Ls[i], -Ds[i]))

# print(adjacency_list)

# BFSする
visited = np.zeros(N, dtype=bool)
weights = np.zeros(N, dtype=int)
to_be_visited = []

# debug 2 exit()
pointer = 0
for pointer in range(N):
    if not visited[pointer]:
        to_be_visited.append(pointer)
        while len(to_be_visited) != 0:
            node_index = to_be_visited.pop()
            candidate = adjacency_list[node_index]
            for _i, weight in candidate:
                if not visited[_i]:
                    visited[_i] = True
                    weights[_i] = weights[node_index] + weight
                    to_be_visited.append(_i)

for i in range(M):
    # print("given weights: {}, estimated weights: {}".format(Ds[i],weights[Rs[i]]-weights[Ls[i]]))
    if weights[Rs[i]] - weights[Ls[i]] == Ds[i]:
        # print("passed {} th condition".format(i))
        pass
    else:
        # print("failed {} th condition".format(i))
        print("No")
        exit()
print("Yes")
### BUGGY CODE ###

N,K=map(int,input().split())
h=list(map(int,input().split()))
INF=float('inf')
dp=[INF]*N
dp[0]=0
for i in range(1,N):
    for j in range(min(i,K)):
        dp[i]=min(dp[i],dp[i-j-1]+abs(h[i]-h[i-j-1]))
print(dp[-1])

### FIXED CODE ###

N,K=map(int,input().split())
h=list(map(int,input().split()))
INF=float('inf')
dp=[INF]*N
dp[0]=0
for i in range(N-1):
    for j in range(1,K+1,1):
        if i+j>N-1:
            break
        dp[i+j]=min(dp[i+j],dp[i]+abs(h[i+j]-h[i]))
print(dp[N-1])