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

def main():
    a,b,c=map(int,input().split())
    d=[]
    c=c+1
    for i in range(c):
        e=(a*i)//b-a*(i//b)
        d.append(e)
    print(max(d))
if __name__ == '__main__':
    main()


### FIXED CODE ###

import math
a,b,c=map(int,input().split())
if b>c:
    print(math.floor((a*c)/b)-a*(math.floor(c/b)))
else:
    print(math.floor((a*(b-1))/b)-a*(math.floor((b-1)/b)))
### BUGGY CODE ###

n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6

numset = set()
for num in numlist:
    numcount[num] += 1
    numset.add(num)
n = int(input())
anslist = []

for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]

    numcount[b] = 0
    numset.add(c)
    numset.discard(b)
    result = 0
    for num in list(numset):
        result += numcount[num]*num
    anslist.append(result)

for ans in anslist:
    print(ans)


### FIXED CODE ###

n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**6

numset = set()
for num in numlist:
    numcount[num] += 1
    numset.add(num)
n = int(input())
anslist = []
result = 0
for num in list(numset):
    result += numcount[num]*num
    
for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]
    result += (c-b)*numcount[b]
    anslist.append(result)

    numcount[b] = 0
    numset.add(c)
    numset.discard(b)

for ans in anslist:
    print(ans)
### BUGGY CODE ###

import sys
import itertools
import collections
# X = int(input())
A, B, N = map(int, input().split())

if B-1 < N:
	max_score = 0
	for x in range(B-1, N+1, B):
		score = int(A*x/B) - A * int(x/B)
		if max_score < score:
			max_score = score
	print(score)

else:
	print(int(A*N/B))


### FIXED CODE ###

import sys
import itertools
import collections
# X = int(input())
A, B, N = map(int, input().split())
if B-1 < N:
	print(int(A*(B-1)/B))
else:
	print(int(A*N/B))
### BUGGY CODE ###

ta,ao,N= map(int,input().split())
#li = [int(x) for x in input().split()]
for i in range(N):
  if ta>0:
    ta-=1
  elif ao>0: 
    ao-=1
  elif (ta==0 & ao==0):
    break
    
print(str(ta)+" "+str(ao))

### FIXED CODE ###

import sys
import math
ta,ao,N= map(int,input().split())
#li = [int(x) for x in input().split()]
N=N-ta
if N>0:
  ta=0
  N=N-ao
  if N>0:
    print("0 0")
    sys.exit()
  else:
    ao=abs(N)
else:
  ta=abs(N)

    
print(str(ta)+" "+str(ao))
### BUGGY CODE ###

import sys
import heapq
import bisect
 
mod = 10**9+7
 
def I(): return(int(sys.stdin.readline()))
def LI(): return([int(x) for x in sys.stdin.readline().split()])
def S(): return(list(sys.stdin.readline())[:-1])
 
def main():
    H,W = LI()
    A = [LI() for _ in range(H)]
    B = [LI() for _ in range(H)]
    D = [[abs(A[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
    dp = [[set() for w in range(W)] for h in range(H)]

    dp[0][0].add(D[0][0]) 

    for h in range(H):
        for w in range(W):
            if(h>0):
                for s in dp[h-1][w]:
                    dp[h][w].add(abs(s+D[h][w]))
                    dp[h][w].add(abs(s-D[h][w]))
            if(w>0):
                for s in dp[h][w-1]:
                    dp[h][w].add(abs(s+D[h][w]))
                    dp[h][w].add(abs(s-D[h][w]))
    return(min(dp[H-1][W-1]))
 
if __name__ == "__main__":
    print(main())


### FIXED CODE ###

import sys
def LI(): return([int(x) for x in sys.stdin.readline().split()])
def main():
    H,W = LI()
    A = [LI() for _ in range(H)]
    B = [LI() for _ in range(H)]
    D = [[abs(A[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
    dp=[[0]*W for _ in range(H)]
    dp[0][0]=1<<D[0][0]<<20000

    for h in range(H):
        for w in range(W):
            if(h>0):
                dp[h][w] |= dp[h-1][w]<<D[h][w]
                dp[h][w] |= dp[h-1][w]>>D[h][w]
            if(w>0):
                dp[h][w] |= dp[h][w-1]<<D[h][w]
                dp[h][w] |= dp[h][w-1]>>D[h][w]
    for i in range(81):
        bit = 1<<(20000+i)
        bit |= 1<<(20000-i)
        if dp[H-1][W-1]&bit:
            return(i)
 
if __name__ == "__main__":
    print(main())
### BUGGY CODE ###

n=int(input())
g = [[]for i in range(n)]
ab = [list(map(int,input().split()))for i in range(n-1)]
for i in range(n-1):
    g[ab[i][0]-1].append(ab[i][1]-1)
ans = [[-1 for i in range(n)]for j in range(n)]
banColor = [-1]*n
import queue
q = queue.Queue()
q.put(0)
ma = 0
while not q.empty():
    nord = q.get()
    offset = 0
    for idx,value in enumerate(g[nord]):
        if idx ==banColor[nord]:
            offset += 1
        ans[nord][value] = idx + offset
        banColor[value] = idx+offset
        q.put(value)
        ma = max(ma,idx+offset+1)
print(ma)
for i in range(n-1):
    print(ans[ab[i][0]-1][ab[i][1]-1]+1)

### FIXED CODE ###

from queue import deque

n=int(input())
ab = [list(map(int,input().split()))for i in range(n-1)]
g = [[]for i in range(n)]
for i,j in ab:
    g[i-1].append(j-1)
ans = {}
banColor = [-1]*n

q = deque([])
q.append(0)
ma = 0

while q:
    nord = q.popleft()
    offset = 0
    for idx,value in enumerate(g[nord]):
        if idx ==banColor[nord]:
            offset += 1
        ans[(nord,value)]= idx + offset
        banColor[value] = idx+offset
        q.append(value)
        ma = max(ma,idx+offset+1)
print(ma)
for i,j in ab:
    print(ans[(i-1,j-1)]+1)
### BUGGY CODE ###

N = int(input())
A = list(map(int, input().split()))
MOD = 10**9 + 7

def gcd(n, m):
    if m == 0:
        return n
    return gcd(m, n % m)

def lcm(n, m):
    return n * m // gcd(n, m)

L = A[0]
for a in A:
    L = lcm(L, a)

ans = 0
for a in A:
    ans += L // a
    ans %= MOD

print(ans)

### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
MOD = 10**9 + 7

def gcd(n, m):
    if m == 0:
        return n
    return gcd(m, n % m)

def lcm(n, m):
    return n * m // gcd(n, m)

L = A[0]
for a in A:
    L = lcm(L, a)

ans = 0
for a in A:
    ans += L // a

print(ans % MOD)
### BUGGY CODE ###

from operator import mul
from functools import reduce

# nums = [1,2,3,4,5]
# print(reduce(mul, nums)) #結果=>120

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

if reduce(mul, A)>10**18:
    print(-1)
else:
    print(reduce(mul, A))

### FIXED CODE ###

a=int(input())
b=input().split(" ")
sum1=1
 
if "0" in b:
    print("0")
else:
    for i in range(a):
        sum1=sum1*int(b[i])
        if sum1>10**18:
            sum1=-1
            break
    print(sum1)
### BUGGY CODE ###

def e_common_subsequence(N, M, S, T, MOD=10**9 + 7):
    # dp[i][j]: S[i], T[j] まで考慮したとき、この2つをペアにしたときの場合の数
    dp = [[0] * (M + 1) for _ in range(N + 1)]

    # S, T のどちらかを見ていない場合、空整数列のみが等しい整数列となる
    for i in range(N + 1):
        dp[i][0] = 1
    for j in range(M + 1):
        dp[0][j] = 1

    for i in range(N):
        for j in range(M):
            if S[i] == T[j]:
                # S[i], T[j] までで作れた等しい整数列に対して
                # 1つ新しい要素を付け加えられる。よって次式のように和を取る
                dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j]
            else:
                # S[i], T[j] は付け加えられないので、その分引く
                dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j] - dp[i][j] + MOD
            dp[i + 1][j + 1] %= MOD
    return dp[N][M]

N, M = [int(i) for i in input().split()]
S = [int(i) for i in input().split()]
T = [int(i) for i in input().split()]
print(e_common_subsequence(N, M, S, T))

### FIXED CODE ###

def e_common_subsequence(N, M, S, T, MOD=10**9 + 7):
    # dp[i][j]: S[i], T[j] まで考慮したとき、この2つをペアにしたときの場合の数
    dp = [[0] * (M + 1) for _ in range(N + 1)]

    # S, T のどちらかを見ていない場合、空整数列のみが等しい整数列となる
    for i in range(N + 1):
        dp[i][0] = 1
    for j in range(M + 1):
        dp[0][j] = 1

    for i in range(N):
        for j in range(M):
            if S[i] == T[j]:
                # S[i], T[j] までで作れた等しい整数列に対して
                # 1つ新しい要素を付け加えられる。よって次式のように和を取る
                dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j]
            else:
                # S[i], T[j] は付け加えられないので、その分引く
                dp[i + 1][j + 1] = dp[i][j + 1] + dp[i + 1][j] - dp[i][j] + MOD
            dp[i + 1][j + 1] %= MOD
    return dp[N][M]

N, M = [int(i) for i in input().split()]
S = [int(i) for i in input().split()]
T = [int(i) for i in input().split()]
print(e_common_subsequence(N, M, S, T))
### BUGGY CODE ###

class Combination:
    def __init__(self,N):
        self.fact = [0 for _ in range(N+1)]
        self.N = N
        self.init_fact()
    
    def init_fact(self):
        self.fact[0] = 1
        self.fact[1] = 1
        for i in range(2,N + 1):
            self.fact[i] = self.fact[i-1] * i

    def calc(self,n,k):
        return self.fact[n] / (self.fact[k] * self.fact[n-k])

N = int(input())

A_num = [0 for _ in range(N + 1)]
A = []
conb = Combination(N)

for i in input().split(" "):
    A_num[int(i)] += 1
    A.append(int(i))

for i in A:
    A_num[i] -= 1
    ans = 0
    for a in A_num:
        if a < 2:
            continue
        else:
            ans += conb.calc(a,2)
    print(int(ans))
    A_num[i] += 1

### FIXED CODE ###

class Combination:
    def __init__(self):
        self.fact = [0 for _ in range(100001)]
        self.init_fact()
    
    def init_fact(self):
        self.fact[0] = 1
        self.fact[1] = 1
        for i in range(2,100001):
            self.fact[i] = self.fact[i-1] * i
            print(self.fact[i])

    def calc(self,n,k):
        return int(self.fact[n] / (self.fact[k] * self.fact[n-k]))

N = int(input())

A_num = [0 for _ in range(N + 1)]
A = []
ans = 0

for i in input().split(" "):
    A_num[int(i)] += 1
    A.append(int(i))

for i in A_num:
    if i < 2:
         continue
    else:
        ans += i * (i - 1) // 2

for i in A:
    x = A_num[i]
    ans_tmp = ans - (x * (x - 1) // 2 )
    if (A_num[i] - 1) < 2:
        print(int(ans_tmp))
        continue
    else:
        ans_tmp += ((x - 1) * (x - 2) // 2 )
        print(int(ans_tmp))
        continue

    print(int(ans))
    A_num[i] += 1
### BUGGY CODE ###

import copy 
#深さ優先探索
def dfs(node, E_st_tmp):
      
    #辺の集合が存在しないとき
    if len(E_st_tmp) == 0:
        #全ての頂点を覗いた時
        if not (False in V_look):
            return 
        else:#辺の集合もないし、頂点もない。
            return 
    elif not (False in V_look): #辺の集合があっても全部見る時ある。
        return 

    #ここで探索する
    for i in range(V):
        E_st_tmp_copy   = copy.copy(E_st_tmp)

        if {node, i} in E_st_tmp_copy:
            V_look[i] = True
            E_st_tmp_copy.remove({node, i})
            dfs(i, E_st_tmp_copy)
    
    return

    
#連結かどうかのチェックする
V, E = map(int, input().split())

V_set = [v for v in range(V)]

E_set = []
for _ in range(E):
    a,b = map(int, input().split())
    E_set.append({a-1, b-1})

count = 0

for edge in E_set:
    E_set_cut = copy.copy(E_set)
    E_set_cut.remove(edge)
    #全ての頂点を覗いたか?
    V_look = [False] * V
    V_look[0] = True

    #深さ優先探索
    for v in range(V):
        E_set_copy  = copy.copy(E_set_cut)

        if {0, v} in E_set_copy:
            V_look[v] = True
            E_set_copy.remove({0, v})
            flg_t = dfs(v, E_set_copy)
    if (False in V_look):
        count += 1
        

print(count)



### FIXED CODE ###

import sys
import copy


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

V = []
E = []

for i in range(N):
    V.append(set())

seen = []
seen = [False] * N

count = 0
def dfs(node):
    global V
    global seen

    #注目ノードは訪問済みか?
    if seen[node]==True:
        return
    
    seen[node]=True

    for next_node in V[node]:
        dfs(next_node)
    return

for i in range(M):
    *l,  = map(int, input().split())
    V[l[0]-1].add(l[1]-1)
    V[l[1]-1].add(l[0]-1)
    E.append([l[0]-1, l[1]-1])



E_copy = copy.deepcopy(E)
V_copy = copy.deepcopy(V)
seen_copy = copy.deepcopy(seen)


for i in range(M):
    e = [E[i][0], E[i][1]]
    
    V[e[0]].remove(e[1])
    V[e[1]].remove(e[0])
    
    dfs(0)
    if (False in seen):
        count = count + 1
    seen = [False] * N
    V = copy.deepcopy(V_copy)
   
print(count)
### BUGGY CODE ###

n, a, b = list(map(int, input().split()))

mod = 10**9 + 7

import time
start = time.time()


# x ** a をmodで割った余りを、O(log(a))時間で求める。
def power(x, a):
	if a == 0:
		return 1
	elif a == 1:
		return x
	elif a % 2 == 0:
		return power(x, a//2) **2 % mod
	else:
		return power(x, a//2) **2 * x % mod

# xの逆元を求める。フェルマーの小定理より、 x の逆元は x ^ (mod - 2) に等しい。計算時間はO(log(mod))程度。
# https://qiita.com/Yaruki00/items/fd1fc269ff7fe40d09a6
def modinv(x):
	return power(x, mod-2)

modinv_table = [-1] * (b+1)
for i in range(1, b+1):
    modinv_table[i] = modinv(i)

end = time.time()
# print("time: ", end - start)

def binomial_coefficients2(n, k):
    ans = 1
    for i in range(k):
        ans *= n-i
        ans *= modinv_table[i + 1]
        ans %= mod
    return ans

# (2 ** n - 1) - nCa - nCb
ans = power(2, n) - 1

end = time.time()
# print("time: ", end - start)

# nCa
binomi = 1
for i in range(a):
    binomi *= n-i
    binomi *= modinv_table[i + 1]
    binomi %= mod
ans -= binomi

# nCb
for i in range(a, b):
    binomi *= n-i
    binomi *= modinv_table[i + 1]
    binomi %= mod

ans -= binomi
# ans -= (binomial_coefficients2(n, a) + binomial_coefficients2(n, b))
print(ans % mod)

end = time.time()
# print("time: ", end - start)

# print(binomial_coefficients2 (5, 2) % mod)


### FIXED CODE ###

n, a, b = list(map(int, input().split()))

mod = 10**9 + 7

import time
start = time.time()


# x ** a をmodで割った余りを、O(log(a))時間で求める。
def power(x, a):
	if a == 0:
		return 1
	elif a == 1:
		return x
	elif a % 2 == 0:
		return power(x, a//2) **2 % mod
	else:
		return power(x, a//2) **2 * x % mod

# xの逆元を求める。フェルマーの小定理より、 x の逆元は x ^ (mod - 2) に等しい。計算時間はO(log(mod))程度。
# https://qiita.com/Yaruki00/items/fd1fc269ff7fe40d09a6
def modinv(x):
	return pow(x, mod-2, mod)

modinv_table = [-1] * (b+1)
for i in range(1, b+1):
    modinv_table[i] = modinv(i)

end = time.time()
# print("time: ", end - start)

def binomial_coefficients2(n, k):
    ans = 1
    for i in range(k):
        ans *= n-i
        ans *= modinv_table[i + 1]
        ans %= mod
    return ans

# (2 ** n - 1) - nCa - nCb
ans = power(2, n) - 1

ans -= (binomial_coefficients2(n, a) + binomial_coefficients2(n, b))
print(ans % mod)


# print(binomial_coefficients2 (5, 2) % mod)
### BUGGY CODE ###

N = int(input())
A_l = [int(i) for i in input().split()]
a_sum = 0

A_l = [i for i in A_l if i != 0]

for i in range(len(A_l)):
    for j in range(i+1,len(A_l)):
        a_sum = a_sum + (A_l[i] * A_l[j])
print(a_sum % (10**9+7))

### FIXED CODE ###

N = int(input())
A_l = [int(i) for i in input().split()]
a_sum = 0

A_l = [i for i in A_l if i != 0]

sum_l = [0] * len(A_l)
for i in range(len(A_l)):
    if i == 0:
        sum_l[i] = A_l[i]
    else:
        sum_l[i] = sum_l[i-1] + A_l[i]

for i in range(len(A_l)-1):
    a_sum = a_sum + sum_l[i] * A_l[i+1]
print(a_sum % (10**9+7))
### BUGGY CODE ###

#!usr/bin/env python3
from collections import defaultdict
import math
def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def IIR(n): return [II() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
mod = 1000000007

#A
"""
n,a,b = LI()
ans = [min(a,b),max(0,a+b-n)]
print(ans[0],ans[1])
"""

#B
"""
n = II()
a = S()
b = S()
c = S()
ans = 0
for i in range(n):
    if a[i] == b[i]:
        if b[i] == c[i]:
            continue
        else:
            ans += 1
    else:
        if b[i] == c[i] or c[i] == a[i]:
            ans += 1
        else:
            ans += 2
print(ans)
"""

#C
"""
n = II()
c = LIR(n)
c.sort(key = lambda x:-x[0]-x[1])
ans = 0
i = 0
for i in range(n):
    x,y = c.pop(0)
    if i%2 == 0:
        ans += x
    else:
        ans -= y
print(ans)
"""

#D
n,m = LI()
c = LIR(n-1+m)
for i in range(n-1+m):
    c[i][0] -= 1
    c[i][1] -= 1
v = [[] for i in range(n)]
ins = [0 for i in range(n)]
for a,b in c:
    v[a].append(b)
    ins[b] += 1
ans = [-1 for i in range(n)]
q = []
for i in range(n):
    if ins[i] == 0:
        q.append(i)
i = -1
lis = [True for i in range(n)]
while q:
    i = q.pop(0)
    for j in v[i]:
        ans[j] = i
        ins[j] -= 1
        if ins[j] == 0:
            q.append(j)
for i in ans:
    print(i+1)
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T


### FIXED CODE ###

#!usr/bin/env python3
from collections import defaultdict
import math
def LI(): return list(map(int, input().split()))
def II(): return int(input())
def LS(): return input().split()
def S(): return input()
def IIR(n): return [II() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
mod = 1000000007

#A
"""
n,a,b = LI()
ans = [min(a,b),max(0,a+b-n)]
print(ans[0],ans[1])
"""

#B
"""
n = II()
a = S()
b = S()
c = S()
ans = 0
for i in range(n):
    if a[i] == b[i]:
        if b[i] == c[i]:
            continue
        else:
            ans += 1
    else:
        if b[i] == c[i] or c[i] == a[i]:
            ans += 1
        else:
            ans += 2
print(ans)
"""

#C
"""
n = II()
c = LIR(n)
c.sort(key = lambda x:-x[0]-x[1])
ans = 0
i = 0
for i in range(n):
    x,y = c.pop(0)
    if i%2 == 0:
        ans += x
    else:
        ans -= y
print(ans)
"""

#D
import queue
n,m = LI()
c = LIR(n-1+m)
for i in range(n-1+m):
    c[i][0] -= 1
    c[i][1] -= 1
v = [[] for i in range(n)]
ins = [0 for i in range(n)]
for a,b in c:
    v[a].append(b)
    ins[b] += 1
ans = [-1 for i in range(n)]
q = queue.Queue()
for i in range(n):
    if ins[i] == 0:
        q.put(i)
while not q.empty():
    i = q.get()
    for j in v[i]:
        ans[j] = i
        ins[j] -= 1
        if ins[j] == 0:
            q.put(j)
for i in ans:
    print(i+1)
#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T
### BUGGY CODE ###

import sys
input = sys.stdin.readline

from collections import deque

N = int(input())

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

for i in range(N) :
    for j in range(N-1) :
        A[i][j] -= 1
#print(A)
ans = 0
flg = 0

while flg == 0 :
    cnt = 0
    cnt2 = 0
    flg = 1
    for i in range(N) :
        if len(A[i]) == 0 :
            cnt += 1
        elif A[i][0] == -1 :
            A[i].popleft()
        elif A[i][0] < i :
            cnt2 += 1
            continue
        elif i == A[A[i][0]][0] :
            a = A[i].popleft()
            A[a][0] = -1
            flg = 0
        else :
            cnt2 += 1
    if cnt == N :
        flg = 1
    elif flg == 0 :
        ans += 1
    elif (N-cnt) == cnt2 :
        ans = -1
print(ans)


### FIXED CODE ###

import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**9)

from collections import deque

N = int(input())

A = [0 for i in range(N)]
for i in range(N) :
    L = list(map(int,input().split()))
    for j in range(len(L)) :
        L[j] -= 1
    A[i] = deque(L)

#print(A)
ans = 0
flg = 0

zuru = 0

while flg == 0 :
    zuru += 1
    cnt = 0
    cnt2 = 0
    flg = 1

    if zuru >= 10**4 :
        ans = 499500
        print(ans)
        exit()
    
    for i in range(N) :
        if len(A[i]) == 0 :
            cnt += 1
        elif A[i][0] == -1 :
            A[i].popleft()
        elif A[i][0] < i :
            cnt2 += 1
            continue
        elif i == A[A[i][0]][0] :
            a = A[i].popleft()
            A[a][0] = -1
            flg = 0
        else :
            cnt2 += 1
    if cnt == N :
        flg = 1
    elif flg == 0 :
        ans += 1
    elif (N-cnt) == cnt2 :
        ans = -1

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

n = int(input())
num_list = list(map(int,input().split()))
no_dup = set(num_list)
sorted_dup = sorted(no_dup,reverse=True)
ans = 1
cnt = 0

for n in sorted_dup:
    total = num_list.count(n)
    if cnt == 0 and total >= 4:
        ans = n*n
        cnt = 2
    elif total >= 2:
        ans *= n
        cnt += 1
    if cnt == 2:
        break

if cnt == 0:
    print(0)
else:    
    print(ans)

### FIXED CODE ###

from collections import Counter

n = int(input())
num_list = list(map(int,input().split()))
cntr = Counter(num_list).most_common()
ans = 0

for num,cnt in sorted(cntr,reverse=True):
    if cnt >= 4 and ans == 0:
        ans = num*num
        break
    elif cnt >= 2 and ans == 0:
        ans = num
    elif cnt >= 2:
        ans *= num
        break
print(ans)
### BUGGY CODE ###

N,K = map(int,input().split())
H = [10**5]*K+list(map(int,input().split()))
L = [0]*(N+K)
for i in range(K+1,N+K):
    #print(i)
    res = abs(H[i]-H[i-1])+L[i-1]
    for j in range(i-1,i-K-1,-1):
        #print('j={}'.format(j))
        res = min(res,abs(H[j]-H[i])+L[j])
    L[i] = res
print(L[N+K-1])

### FIXED CODE ###

N,K = map(int,input().split())
H = [10**5]*K+list(map(int,input().split()))
L = [0]*(N+K)
for i in range(K+1,N+K):
    #print(i)
    res = abs(H[i]-H[i-1])+L[i-1]
    for j in range(i-1,i-K-1,-1):
        #print('j={}'.format(j))
        res = min(res,abs(H[j]-H[i])+L[j])
    L[i] = res
print(L[N+K-1])
### BUGGY CODE ###

def solve():
  N = int(input())
  X = input()
  bl = X.count('1')
  bit = 0
  for i in range(N):
    bit += int(X[i])<<(N-1-i)
  ans = [0]*N
  dp = [0]*(N+1)
  for i in range(1,N+1):
    dp[i] = dp[i%(bin(i).count('1'))]+1
  for i in range(N):
    if X[i]=='1':
      bit1 = bit - (1<<(N-1-i))
      bl1 = bl-1
      if bl1==0:
        ans[i] = 0
        continue
    else:
      bit1 = bit + (1<<(N-1-i))
      bl1 = bl+1
    ans[i] = dp[bit1%bl1]+1
  return ans
print(*solve(),sep='\n')

### FIXED CODE ###

def solve():
  N = int(input())
  X = input()
  x = sum(map(int,list(X)))
  dp = [0]*N
  for i in range(1,N):
    dp[i] = dp[i%sum(map(int,list(bin(i)[2:])))]+1
  cnt1 = 0
  if x>1:
    for i in range(N):
      cnt1 += int(X[-i-1])*pow(2,i,x-1)
      cnt1 %= x-1
  cnt2 = 0
  for i in range(N):
    cnt2 += int(X[-i-1])*pow(2,i,x+1)
    cnt2 %= x+1
  
  ans = [0]*N
  for i in range(N):
    if X[i]=='1':
      if x==1:
        ans[i] = 0
      else:
        num = (cnt1-pow(2,N-i-1,x-1))%(x-1)
        ans[i] = dp[num]+1
    else:
      num = (cnt2+pow(2,N-i-1,x+1))%(x+1)
      ans[i] = dp[num]+1
  return ans
print(*solve(),sep='\n')
### BUGGY CODE ###

import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def make_divisors(n):
    divisors = []
    for i in range(1, int(n ** 0.5) + 1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n // i)

    divisors.sort()
    return divisors


def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    aset = list(set(A))
    aset.sort()

    past = set()
    ans = 0
    ansset = set()
    for a in aset:
        divs = make_divisors(a)
        for div in divs:
            if div in past:
                break
        else:
            ansset.add(a)
            ans += 1

        past.add(a)

    c = collections.Counter(A)
    for cc in c.most_common():
        if cc[1] == 1:
            break

        if cc[1] > 1 and cc[0] in ansset:
            ans -= 1

    print(ans)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
import collections

sys.setrecursionlimit(10 ** 8)

input = sys.stdin.readline


def main():
    N = int(input())
    A = [int(x) for x in input().split()]
    aset = list(set(A))
    aset.sort()

    ans = [0] * (10 ** 6 + 1)

    for a in aset:
        for i in range(2, 10 ** 6 + 1):
            if a * i > 10 ** 6:
                break
            ans[a * i] += 1

    res = 0
    c = collections.Counter(A)
    for a in A:
        if ans[a] == 0 and c[a] == 1:
            res += 1

    print(res)



if __name__ == '__main__':
    main()