Harryxun/stf_run huggingface dataset:
N,M = map(int, input().split())

room_and_connecting_room_dict = {i:set() for i in range(1,N+1)}

for i in range(M):
    A,B = map(int, input().split())
    room_and_connecting_room_dict[A].add(B)
    room_and_connecting_room_dict[B].add(A)

def get_child_ans(room_numbers_set): #room_number_list:前の部屋番の子ども全て
    
    next_room_numbers_set = set()
    
    for room_number in room_numbers_set:
        for child_room in room_and_connecting_room_dict[room_number]:
            if child_room not in calculated_room_numbers_set:
                ans_dict[child_room] = room_number #前の部屋番の子供は全て、前の部屋番に回答が確定する
                next_room_numbers_set.add(child_room)
                calculated_room_numbers_set.add(child_room)
 
    #終了判定
    if len(calculated_room_numbers_set) == N: #全部計算済み
        return 1
    elif len(next_room_numbers_set) == 0: #1から繋がる全てを計算したのに未計算がある
        return -1
    else: #孫がいるのでまだやる
        return get_child_ans(next_room_numbers_set)

ans_dict = dict()
ans_dict[1] = 0
calculated_room_numbers_set = set()
calculated_room_numbers_set.add(1)

is_there_ans = get_child_ans(set([1]))  

if is_there_ans == 1:
    print("Yes")
    for i in range(2,N+1):
        print(ans_dict[i])
else:
    print("No")

### FIXED CODE ###

import sys
sys.setrecursionlimit(1000000)

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

room_and_connecting_room_dict = {i:set() for i in range(1,N+1)}

for i in range(M):
    A,B = map(int, input().split())
    room_and_connecting_room_dict[A].add(B)
    room_and_connecting_room_dict[B].add(A)

def get_child_ans(room_numbers_set): #room_number_list:前の部屋番の子ども全て
    
    next_room_numbers_set = set()
    
    for room_number in room_numbers_set:
        for child_room in room_and_connecting_room_dict[room_number]:
            if child_room not in calculated_room_numbers_set:
                ans_dict[child_room] = room_number #前の部屋番の子供は全て、前の部屋番に回答が確定する
                next_room_numbers_set.add(child_room)
                calculated_room_numbers_set.add(child_room)
 
    #終了判定
    if len(calculated_room_numbers_set) == N: #全部計算済み
        return 1
    elif len(next_room_numbers_set) == 0: #1から繋がる全てを計算したのに未計算がある
        return -1
    else: #孫がいるのでまだやる
        return get_child_ans(next_room_numbers_set)

ans_dict = dict()
ans_dict[1] = 0
calculated_room_numbers_set = set()
calculated_room_numbers_set.add(1)

is_there_ans = get_child_ans(set([1]))  

if is_there_ans == 1:
    print("Yes")
    for i in range(2,N+1):
        print(ans_dict[i])
else:
    print("No")
def main2():
    N, A, B = map(int, input().split())

    d = B - A - 1
    if d % 2 ==  0:
        print(min(B - 1, N - A))
    else:
        print((d + 1)//2)

if __name__ == "__main__":
    main()

### FIXED CODE ###

def main3():
    N, A, B = map(int, input().split())
    
    d = B - A
    if d % 2 == 0:
        print(d // 2)
    else:
        print(min(((d + 1) // 2 + N - B ), ((d + 1) // 2 + A - 1)))
         
if __name__ == "__main__":
    main3()
A, B, C = map(int, raw_input().split())

if (C % A) == 0 && (B % A) == 0 or \
        (C % A) % (B % A) == 0:
    print('YES')
else:
    print('NO')


### FIXED CODE ###

import sys

A, B, C = map(int, raw_input().split())

for i in range(1, 101):
    if C == 0 and (B * i % A) == 0 or \
            (C % A) == 0 and (B * i % A) == 0 or \
            (B * i % A) != 0 and (C % A) % (B * i % A) == 0:
        print('YES')
        sys.exit()

print('NO')
#-*- coding: utf-8 -*-
N = str(input())
for i in range(1,2):
  if N[0]==N[1]==N[2] or N[1]==N[2]==N[3]

### FIXED CODE ###

#-*- coding: utf-8 -*-
N = str(input())
if N[0]==N[1]==N[2] or N[1]==N[2]==N[3]:
  print("Yes")
else:
  print("No")
a = input()
b = 'Sunny'
c = 'Cloudy'
d = 'Rainy'
if a = b:
 print(c)
if a = c:
 print(d)
if a = d:
 print(b)


### FIXED CODE ###

a = input()
b = 'Sunny'
c = 'Cloudy'
d = 'Rainy'
if a == b:
 print(c)
if a == c:
 print(d)
if a == d:
 print(b)
import math
while True:
    n = int(input())
    score = [ int(i) for i in input().split(" ") ] 
    average = sum(score) / n
    if n == 0:
        break
    a2 = 0
    for s in score:
        a2 += (s - average) ** 2

    print("{:.8f}".format(math.sqrt(a2) / n ), end ="\n")

### FIXED CODE ###

import math
while True:
    n = int(input())
    if n == 0:
        break
    s = [int(i) for i in input().split(" ")]
    m = sum(s) / n
    print("{:.8f}".format(math.sqrt(sum([(x-m) ** 2 for x in s]) / n)))
import itertools

H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]

ans = float("inf")
for n_horizontal_cut in range(H):
    for horizontal_cut_idx in list(itertools.combinations(list(range(1,H)), n_horizontal_cut)):
        horizontal_cut_idx = [0] + list(horizontal_cut_idx) + [H]
        n_vertical_cut = 0
        n_white_choco = [0] * (n_horizontal_cut+1)
        for w in range(W):
            tmp_idx = 0
            tmp_n_white_choco = n_white_choco.copy()
            for h in range(H):
                if h >= horizontal_cut_idx[tmp_idx+1]:
                    tmp_idx += 1
                if S[h][w] == 1:
                    n_white_choco[tmp_idx] += 1
            if max(n_white_choco) > K:
                n_vertical_cut += 1
                n_white_choco = [n_white_choco[i] - tmp_n_white_choco[i] for i in range(n_horizontal_cut+1)]
        ans = min(ans, n_horizontal_cut + n_vertical_cut)

print(ans)

### FIXED CODE ###

import itertools, copy

H, W, K = map(int, input().split())
S = [list(map(int, list(input()))) for _ in range(H)]

ans = float("inf")
for n_horizontal_cut in range(H):
    for horizontal_cut_idx in list(itertools.combinations(list(range(1,H)), n_horizontal_cut)):
        horizontal_cut_idx = [0] + list(horizontal_cut_idx) + [H]
        n_vertical_cut = 0
        n_white_choco = [0] * (n_horizontal_cut+1)
        for w in range(W):
            tmp_idx = 0
            tmp_n_white_choco = copy.copy(n_white_choco)
            for h in range(H):
                if h >= horizontal_cut_idx[tmp_idx+1]:
                    tmp_idx += 1
                if S[h][w] == 1:
                    n_white_choco[tmp_idx] += 1
            if max(n_white_choco) > K:
                n_vertical_cut += 1
                n_white_choco = [n_white_choco[i] - tmp_n_white_choco[i] for i in range(n_horizontal_cut+1)]
                if max(n_white_choco) > K: # 1列の区間でホワイトチョコ数がオーバーする場合
                    break
        else:
            ans = min(ans, n_horizontal_cut + n_vertical_cut)

print(ans)
mod = 998244353
N,A,B,K = map(int,input().split())
def cmb(n,k):
    return fac[n]*inv_fac[n-k]*inv_fac[k] % mod
fac = [1]
for i in range(1,N+1):
    fac.append(fac[-1]*i % mod)
inv_fac = [pow(fac[N],mod-2,mod)]
for i in range(1,N+1)[::-1]:
    inv_fac.append(inv_fac[-1]*i % mod)
inv_fac = inv_fac[::-1]

ans = 0
for i in range(N+1):
    if i*A > K:
        break
    if (K-i*A) % B == 0:
        j = (K-i*A)//B
        ans += cmb(N,i)*cmb(N,j)
        ans %= mod
print(ans)

### FIXED CODE ###

mod = 998244353
N,A,B,K = map(int,input().split())
def cmb(n,k):
    return fac[n]*inv_fac[n-k]*inv_fac[k] % mod
fac = [1]
for i in range(1,N+1):
    fac.append(fac[-1]*i % mod)
inv_fac = [pow(fac[N],mod-2,mod)]
for i in range(1,N+1)[::-1]:
    inv_fac.append(inv_fac[-1]*i % mod)
inv_fac = inv_fac[::-1]

ans = 0
for i in range(N+1):
    if i*A > K:
        break
    if (K-i*A) % B == 0:
        j = (K-i*A)//B
        if j <= N:
            ans += cmb(N,i)*cmb(N,j)
            ans %= mod
print(ans)
I=lambda:list(map(int.input().split()));n,m=I();p=I();l=[i for i in range(n+1)]
def u(x):
 if l[x]!=x:l[x]=u(l[x])
 return l[x]
for _ in[0]*m:x,y=I();l[u(x)]=u(y)
print(sum(u(i)==u(j)for i,j in enumerate(p,1)))

### FIXED CODE ###

class UnionFind():
  def __init__(self,size):
    self.table=[-1for _ in range(size)]
  def root(self,x):
    while self.table[x]>=0:
      x=self.table[x]
    return x
  def unite(self,x,y):
    s1=self.root(x)
    s2=self.root(y)
    if s1!=s2:
      if self.table[s1]!=self.table[s2]:
        if self.table[s1]<self.table[s2]:
          self.table[s2]=s1
        else:
          self.table[s1]=s2
      else:
        self.table[s1]-=1
        self.table[s2]=s1
    return
  def same(self,x,y):
    return self.root(x)==self.root(y)
I=lambda:list(map(int,input().split()))
n,m=I()
p=I()
u=UnionFind(n)
for _ in range(m):
  a,b=I()
  u.unite(a-1,b-1)
print(sum(u.same(i,j-1)for i,j in zip(range(n),p)))
= int(input())
a = list(map(int, input().split()))
b = list(map(int, input().split()))
ans = 0

for i in range(n):
  if a[i] < b[i]:
    ab = b[i] - a[i]
    if a[i+1] > ab:
      ans += b[i]
      a[i+1] -= ab
    else:
      ans += a[i] + a[i+1]
      a[i+1] = 0
  else:
    ans += b[i]

print(ans)


### FIXED CODE ###

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

for i in range(n):
  if a[i] < b[i]:
    ab = b[i] - a[i]
    if a[i+1] > ab:
      ans += b[i]
      a[i+1] -= ab
    else:
      ans += a[i] + a[i+1]
      a[i+1] = 0
  else:
    ans += b[i]

print(ans)
import sys
sys.setrecursionlimit(10**7)


n=int(input())
X=str(input())

number=0
for i in range(n):
    if X[i]=='1':
        number+=1

base1=0
for i in range(n):
    if X[i]=='1':
        base1=(base1+pow(2,n-i-1,number+1))%(number+1)


base2=0
for i in range(n):
    if X[i]=='1':
        base2=(base2+pow(2,n-i-1,number-1))%(number-1)



def culc(x):
    bin_x=str(bin(x)[2:])
    one=0
    for i in range(len(bin_x)):
        if bin_x[i]=='1':
            one+=1
    return x%one
        



#dp[i](0<=i<=number)
dp=[-1]*(number+1)
dp[0]=0
def rec(x):
    if dp[x]>=0:
        return dp[x]
    else:
        dp[x]=rec(culc(x))+1
        return dp[x]





for i in range(n):
    #base1
    if X[i]=='0':
        temp=(base1+pow(2,n-i-1,number+1))%(number+1)
        ans=rec(temp)+1
        print(ans)
    #base2
    if X[i]=='1':
        temp=(base2-pow(2,n-i-1,number-1))%(number-1)
        ans=rec(temp)+1
        print(ans)



### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)


n=int(input())
X=str(input())

number=0
for i in range(n):
    if X[i]=='1':
        number+=1



def culc(x):
    bin_x=str(bin(x)[2:])
    one=0
    for i in range(len(bin_x)):
        if bin_x[i]=='1':
            one+=1
    return x%one
        



#dp[i](0<=i<=number)
dp=[-1]*(number+1)
dp[0]=0
def rec(x):
    if dp[x]>=0:
        return dp[x]
    else:
        dp[x]=rec(culc(x))+1
        return dp[x]



if number==0:
    for i in range(n):
        print(1)
    sys.exit()



base1=0
for i in range(n):
    if X[i]=='1':
        base1=(base1+pow(2,n-i-1,number+1))%(number+1)

if number==1:
    for i in range(n):
        if X[i]=='1':
            print(0)
        else:
            temp=(base1+pow(2,n-i-1,number+1))%(number+1)
            ans=rec(temp)+1
            print(ans)
    sys.exit()



base2=0
for i in range(n):
    if X[i]=='1':
        base2=(base2+pow(2,n-i-1,number-1))%(number-1)






for i in range(n):
    #base1
    if X[i]=='0':
        temp=(base1+pow(2,n-i-1,number+1))%(number+1)
        ans=rec(temp)+1
        print(ans)
    #base2
    if X[i]=='1':
        temp=(base2-pow(2,n-i-1,number-1))%(number-1)
        ans=rec(temp)+1
        print(ans)
#! /usr/bin/env python3

from fractions import gcd
# from math import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

import copy

N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for i in range(M)]

road = [ [] for i in range(N) ]

for ab in AB:
    road[ab[0]-1] += [ab[1]-1]
    road[ab[1]-1] += [ab[0]-1]

print(road)
rets = [N for i in range(N)]

def dfs(start, num, ct, done):
    global rets, road
    
    print(start, done, num, ct)
    if num == 0:
        rets[start] = min(rets[start], ct)

    if done[num]:
        return
    done[num] = 1
    

    
    ct += 1
    next = road[num]
    for i in next:
        dfs(start, i, ct, copy.copy(done))

for start in range(1, N):
    ct = 0
    done = [0 for i in range(N)]
    dfs(start, start, ct, done)

print("Yes")
for i in rets[1:]:
    print(i)

### FIXED CODE ###

#! /usr/bin/env python3

# from math import gcd
from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement
import copy

N, M = [int(_) for _ in input().split()]
AB = [[int(_) for _ in input().split()] for i in range(M)]

graph = [[] for i in range(N)]

for A, B in AB:
    graph[A-1] += [B]
    graph[B-1] += [A]

D = deque([0])
visited = [0] * N
visited[0] = 1

ret = [0 for i in range(N)]

while D:
    v = D.popleft()
    for i in graph[v]:
        i -= 1
        if visited[i]: continue
        visited[i] = 1
        ret[i] = v + 1
        D.append(i)

if any(ret[1:]):
    print("Yes")
    for i in ret[1:]:
        print(i)
else:
    print("No")
print(input() * 2 * 3.14)

### FIXED CODE ###

import math
print(int(input())*2 * math.pi)
import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1
    
def multiply(x,y):
    ans = x*y
    if ans>=10**9+7:
        ans %= 10**9+7
    return ans

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

if A[-1]<0 and K%2==1:
    combi = A[-K:]
    answer =  functools.reduce(multiply,map(abs,combi[:-1]))
    answer *= combi[-1]
    answer %= 10**9+7
else:
    signs = 1
    i,j = 0,1
    for _ in range(K):
        if abs(A[i]) > abs(A[N-j]):
            signs *= sign(A[i])
            i += 1
            
        else:
            signs *= sign(A[N-j])
            j += 1
    combi = A[N-j+1:] + A[:i]
    del A[-j+1:]
    del A[:i]
    combi = combi[::-1]     
       
    if signs == -1:
        if combi[0] <0:
            del combi[-1]
            combi.append(A[-1])
        else:
            if A[0]>0:
                del combi[-1]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[-1]*A[0] > combi[0]*A[-1]:
                    del combi[0]
                    combi.insert(0,A[0])
                else:
                    del combi[-1]
                    combi.append(A[-1])
            else:
                del combi[0]
                combi.insert(0,A[0])

    answer =  functools.reduce(multiply,map(abs,combi))
print(answer)

### FIXED CODE ###

import functools

def sign(x):
    if x>0:
        return 1
    elif x==0:
        return 0
    else:
        return -1
    
def multiply(x,y):
    ans = x*y
    if ans>=10**9+7 or ans<0:
        ans %= (10**9+7)
    return ans

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

if (A[-1]<0 and K%2==1):
    combi = A[-K:]
    if K >1:
        answer =  functools.reduce(multiply,combi)

elif K == N:
    answer = functools.reduce(multiply,A)
    
else:
    signs = 1
    i,j = 0,0
    for _ in range(K):
        if abs(A[i]) > abs(A[N-j-1]):
            signs *= sign(A[i])
            i += 1
            
        else:
            signs *= sign(A[N-j-1])
            j += 1
    combi = A[N-j:] + A[:i]
    del A[N-j:]
    del A[:i]
    combi = combi[::-1]     
       
    if signs == -1:
        if A:
            if combi[-1]<0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]>=0:
                del combi[0]
                combi.append(A[-1])
            elif A[0]<0 and A[-1] >0:
                if combi[0]*A[0] > combi[-1]*A[-1]:
                    del combi[-1]
                    combi.insert(0,A[0])
                else:
                    del combi[0]
                    combi.append(A[-1])
            elif A[-1] <= 0:
                del combi[-1]
                combi.insert(0,A[0]) 
                
    answer =  functools.reduce(multiply,combi)
print(answer)
a = int(input())
b = [False] *(10**5)
for i in range(a):
    c = int(input())
    b[c-1] = not b[c-1]
d = 0
for i in b:
    if i:
        d += 1
print(d)


### FIXED CODE ###

a = int(input())
b = {}
for i in range(a):
    c = int(input())
    if c not in b:
        b[c] = 1
    else:
        b[c] += 1
c = 0
for k, v in b.items():
    if v % 2 != 0:
        c += 1
print(c)
N,i = list(map(int,input()))
print(N-i+1)


### FIXED CODE ###

N,i = list(map(int,input().split()))
print(N - i + 1)
import sys


def parallelism(x1, y1, x2, y2, x3, y3, x4, y4):
    if x1 == x2:
        return x3 == x4
    elif y1 == y2:
        return y3 == y4
    else:
        return (y2 - y1) / (x2 - x1) == (y4 - y3) / (x4 - x3)



#input_file = open(sys.argv[1], "r")

#lines = input_file.readlines()
lines = sys.stdin.readlines()
lines.pop(0)

for line in lines:
    x1, y1, x2, y2, x3, y3, x4, y4 = tuple(map(float, line.split(' ')))
    if parallelism(x1, y1, x2, y2, x3, y3, x4, y4):
        print "YES"
    else:
        print "NO"

### FIXED CODE ###


import sys


def parallelism(x1, y1, x2, y2, x3, y3, x4, y4):
    if x2 - x1 == 0.0 or x4 - x3 == 0.0:
        return x2 - x1 == 0.0 and x4 - x3 == 0.0
    else:
        return ((y2 - y1) / (x2 - x1)) == ((y4 - y3) / (x4 - x3))


#lines = sys.stdin.readlines()
#lines.pop(0)

for i in range(input()):
    x1, y1, x2, y2, x3, y3, x4, y4 = tuple(map(float, raw_input().split(' ')))
    if parallelism(x1, y1, x2, y2, x3, y3, x4, y4):
        print "YES"
    else:
        print "NO"
import numpy as np
h, w = map(int,input().split())
f = lambda:list(map(str,input().split()))
g = []
for i in range(h):
  li = f()
  g.append(li)
#print(g)
dp = np.zeros((h,w))
dp[0][0] = 1
#print(g[1][0] == '.')
#print(g[1][0])
#print(g[0][3])
for i in range(h):
  for j in range(w):
    if i+1<h and g[i+1][j] == '.':
      dp[i+1][j] = dp[i+1][j] + dp[i][j]
    if j+1<w and g[i][j+1] == '.':
      dp[i][j+1] =dp[i][j+1] + dp[i][j]
print(dp)
ans = int(dp[-1][-1])
print(ans%(10**9+7))

### FIXED CODE ###

h, w = map(int,input().split())
g = [list(input()) for _ in range(h)]
#print(g)
dp = [[0]*w for _ in range(h)]
dp[0][0] = 1
#print(g[1][0] == '.')
#print(g[1][0])
#print(g[0][3])
mod = 10**9+7
for i in range(h):
  for j in range(w):
    if i+1<h and g[i+1][j] == '.':
      dp[i+1][j] = (dp[i+1][j] + dp[i][j])%mod
    if j+1<w and g[i][j+1] == '.':
      dp[i][j+1] = (dp[i][j+1] + dp[i][j])%mod
print(dp[-1][-1])
n, m, c = map(int, input().split())
b = input().split()
ans = 0

for j in range(n):
    a = input().split()
    for i in range(m):
        sum = 0
        sum += a[i]*b[i]
    if sum > -c:
        ans += 1

print(ans)

### FIXED CODE ###

n, m, c = map(int, input().split())
b = list(int(x) for x in input().split())
ans = 0

for j in range(n):
    a = list(int(x) for x in input().split())
    sum = 0
    for i in range(m):
        sum += a[i]*b[i]
    if sum > -c:
        ans += 1

print(ans)
import numpy as np

def calc(n):
    if n < 2:
        return 0
#     lim = int((n - 2) ** 0.5 - 1) + 1
    lim=130

    result=np.zeros(n+1,dtype=int)
    for x in range(1, lim):
        for y in range(1, lim):
            if x > y:
                continue
            for z in range(1, lim):
                if y > z:
                    continue
                index = x ** 2 + y ** 2 + z ** 2 + x * y + y * z + z * x
                if index>n:continue
                s = set([x, y, z])
                if len(s) == 1:
                    result[index] += 1
                elif len(s) == 2:
                    result[index] += 3
                else:
                    result[index] += 6

    return result[1:]

for i in calc(int(input())):
    print(i)

### FIXED CODE ###

import numpy as np


def calc(n):
    #     lim = int((n - 2) ** 0.5 - 1) + 1
    lim = 130

    result = np.zeros(n + 1, dtype=int)
    for x in range(1, lim):
        for y in range(1, lim):
            if x > y:
                continue
            for z in range(1, lim):
                if y > z:
                    continue
                index = x ** 2 + y ** 2 + z ** 2 + x * y + y * z + z * x
                if index > n:
                    continue
                s = set([x, y, z])
                if len(s) == 1:
                    result[index] += 1
                elif len(s) == 2:
                    result[index] += 3
                else:
                    result[index] += 6

    return result[1:]


for i in calc(int(input())):
    print(i)
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    ans=0
    for i n range(n):
        l,r=INTM()
        ans+=r-l+1
    print(ans)
if __name__ == '__main__':
    do()

### FIXED CODE ###

INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))
def do():
    n=INT()
    ans=0
    for i in range(n):
        l,r=INTM()
        ans+=r-l+1
    print(ans)
if __name__ == '__main__':
    do()
n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [float('inf')] * n

dp[0] = 0
for i in range(n):
  for j in range(k+1):
    dp[i] = min(dp[i], dp[i-j] + abs(h[i]-h[i-j]))
    
    
print(dp[n-1])

### FIXED CODE ###

n, k = map(int, input().split())
h = list(map(int, input().split()))

dp = [float('inf')] * n

dp[0] = 0
for i in range(1, n):
  dp[i] = min(dp[j]+abs(h[i]-h[j]) for j in range(max(0, i-k), i))

    
print(dp[n-1])
A, B = map(int, input().split())
import math

print(A * B // math.gcd(A, B))

### FIXED CODE ###

A, B = map(int, input().split())
from fractions import gcd
 
print(A * B // gcd(A, B))
n = int(input())
l = []
for i in range(n):
    l.append(int(input()))
m = max(l)
x = sorted(set(l))[-2]
ans = [m]*n
a = [index for index, value in enumerate(l) if value == m]
for i in a:
    ans[i] = x
[print(i) for i in ans]


### FIXED CODE ###

n = int(input())
l = []
for i in range(n):
    l.append(int(input()))
m = max(l)
ans = [m]*n
if len(set(l)) == 1 or l.count(m) > 1:
    for i in ans:
        print(i)
else:
    x = sorted(set(l))[-2]
    a = l.index(m)
    ans[a] = x
    for i in ans:
        print(i)
I = input
pr = print
n = int(I())
ar = [int(x) for x in I().split()]
ar.append(0)
dp = [0]*(n+1)
dp[n-1]=ar[-1]
def solve(i):
	if i>=(n-1): return dp[i];
	if not dp[i]: dp[i] = min(abs(ar[i]-ar[i+1])+solve(i+1),abs(ar[i]-ar[i+2])+solve(i+2))
	return dp[i];
pr(solve(0))

### FIXED CODE ###

import sys
I = lambda:map(int,sys.stdin.readline().split())
pr = lambda x:sys.stdout.write(f'{x}\n')
sys.setrecursionlimit(2000000001)

mn = lambda w,x,y,z: min(abs(w)+x,abs(y)+z)
n = ar = dp = 0

def solve(x):
	global n,ar,dp
	if x>=n-2: return dp[x]
	if dp[x] != -1: return dp[x]
	dp[x] = mn(ar[x]-ar[x+1], solve(x+1), ar[x]-ar[x+2], solve(x+2))
	# pr(f'{x} => {dp[x]}')
	return dp[x]

def main():
	global n,ar,dp
	n, ar = *I(), [*I()]
	dp = [-1]*(n-2) + [abs(ar[-2]-ar[-1]),0]
	# pr((n,ar,dp))
	pr(solve(0))

main()
from bisect import bisect

s = input()
t = input()

d = dict()

for i in range(26):
    d[chr(i+96)] = []

for i, si in enumerate(s):
    d[si].append(i)

ans = 0
res = -1

for i in range(len(t)):
    if d[t[i]] == []:
        print(-1)
        exit()
    index = bisect(d[t[i]], res)
    if index == len(d[t[i]]):
        ans += len(s)
        res = d[t[i]][0]
    else:
        res = d[t[i]][index]

ans += d[t[-1]][0] + 1

print(ans)

### FIXED CODE ###

from bisect import bisect

s = input()
t = input()

d = dict()

for i in range(26):
    d[chr(i+97)] = []

for i, si in enumerate(s):
    d[si].append(i)

ans = 0
res = -1

for i in range(len(t)):
    if d[t[i]] == []:
        print(-1)
        exit()
    index = bisect(d[t[i]], res)
    if index == len(d[t[i]]):
        ans += len(s) - res + d[t[i]][0]
        res = d[t[i]][0]
    else:
        ans += d[t[i]][index] - res
        res = d[t[i]][index]

print(ans)