HDD hard drives group data by sectors. All files are split to fragments and each of them are written in some sector of hard drive. Note the fragments can be written in sectors in arbitrary order.

One of the problems of HDD hard drives is the following: the magnetic head should move from one sector to another to read some file.

Find the time need to read file split to n fragments. The i-th sector contains the f_{i}-th fragment of the file (1 ≤ f_{i} ≤ n). Note different sectors contains the different fragments. At the start the magnetic head is in the position that contains the first fragment. The file are reading in the following manner: at first the first fragment is read, then the magnetic head moves to the sector that contains the second fragment, then the second fragment is read and so on until the n-th fragment is read. The fragments are read in the order from the first to the n-th.

It takes |a - b| time units to move the magnetic head from the sector a to the sector b. Reading a fragment takes no time.


-----Input-----

The first line contains a positive integer n (1 ≤ n ≤ 2·10^5) — the number of fragments.

The second line contains n different integers f_{i} (1 ≤ f_{i} ≤ n) — the number of the fragment written in the i-th sector.


-----Output-----

Print the only integer — the number of time units needed to read the file.


-----Examples-----
Input
3
3 1 2

Output
3

Input
5
1 3 5 4 2

Output
10



-----Note-----

In the second example the head moves in the following way:  1->2 means movement from the sector 1 to the sector 5, i.e. it takes 4 time units  2->3 means movement from the sector 5 to the sector 2, i.e. it takes 3 time units  3->4 means movement from the sector 2 to the sector 4, i.e. it takes 2 time units  4->5 means movement from the sector 4 to the sector 3, i.e. it takes 1 time units 

So the answer to the second example is 4 + 3 + 2 + 1 = 10.

Difficulty: EASY
Tags: Mathematics

Implementation

Starter code:

Solutions:
n = int(input())
f = list(map(int, input().split()))
a = [0] * (n + 1)
for i in range(n):
	a[f[i]] = i + 1
res = 0
for i in range(1, n):
	res += abs(a[i + 1] - a[i])
print(res)


n = int(input())
numbers = [int(x) - 1 for x in input().split()]
position = [0] * n
for i in range(0, n):
	position[numbers[i]] = i
res = 0
for i in range(1, n):
	res += abs(position[i - 1] - position[i])
print(res)


def steps(arr):
	index_arr = []
	for (i, a) in enumerate(arr):
		index_arr.append((a, i))
	index_arr = sorted(index_arr, key=lambda x: x[0])
	steps = 0
	for (a, i) in index_arr[1:]:
		steps += abs(i - index_arr[a - 2][1])
	return steps
_ = input()
values = input()
value_list = []
for v in values.split(' '):
	value_list.append(int(v))
print(steps(value_list))


n = int(input())
f = list(map(int, input().split()))
f_to_pos = [0] * n
for (i, fr) in enumerate(f):
	f_to_pos[fr - 1] = i
total = 0
for i in range(n - 1):
	total += abs(f_to_pos[i] - f_to_pos[i + 1])
print(total)


n = int(input())
f = list(map(int, input().split()))
f2 = list(range(n))
for i in range(n):
	f2[f[i] - 1] = i
sum = 0
for i in range(0, n - 1):
	sum += abs(f2[i] - f2[i + 1])
print(sum)


def STR():
	return list(input())

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def MAP2():
	return map(float, input().split())

def LIST():
	return list(map(int, input().split()))

def STRING():
	return input()
import string
import sys
from heapq import heappop, heappush
from bisect import *
from collections import deque, Counter, defaultdict
from math import *
from itertools import permutations, accumulate
dx = [-1, 1, 0, 0]
dy = [0, 0, 1, -1]
n = INT()
arr = LIST()
d = {}
for i in range(n):
	d[arr[i]] = i + 1
i = 1
ans = 0
while i < n:
	x = abs(d[i] - d[i + 1])
	ans += x
	i += 1
print(ans)


n = int(input())
a = [0] * n
for (i, x) in enumerate(map(int, input().split())):
	a[x - 1] = i
print(sum((abs(a[i + 1] - a[i]) for i in range(n - 1))))


n = int(input())
fi = list(map(int, input().split()))
fi2 = list(range(n))
i = 0
while i < n:
	fi2[fi[i] - 1] = i
	i += 1
pos = fi2[0]
i = 1
d = 0
while i < n:
	d += abs(fi2[i] - pos)
	pos = fi2[i]
	i += 1
print(d)


n = int(input())
f = list(map(int, input().split()))
a = [None] * n
for i in range(n):
	a[f[i] - 1] = i
ans = sum((abs(a[i] - a[i - 1]) for i in range(1, n)))
print(ans)


n = int(input())
a = list(map(int, input().split()))
p = 0
i = 0
x = {}
while i < n:
	x[a[i]] = i
	i += 1
while n > 1:
	p = p + abs(x[n] - x[n - 1])
	n -= 1
print(p)


N = int(input())
A = list(map(int, input().split()))
D = [0 for _ in range(N)]
for i in range(N):
	D[A[i] - 1] = i
res = sum([abs(D[i + 1] - D[i]) for i in range(N - 1)])
print(res)


import sys
import bisect
from bisect import bisect_left as lb
input_ = lambda : sys.stdin.readline().strip('\r\n')
from math import log
from math import gcd
from math import atan2, acos
from random import randint
sa = lambda : input_()
sb = lambda : int(input_())
sc = lambda : input_().split()
sd = lambda : list(map(int, input_().split()))
se = lambda : float(input_())
sf = lambda : list(input_())
flsh = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7
gp = []
cost = []
dp = []
mx = []
ans1 = []
ans2 = []
special = []
specnode = []
a = 0
kthpar = []

def dfs(root, par):
	if par != -1:
		dp[root] = dp[par] + 1
	for i in range(1, 20):
		if kthpar[root][i - 1] != -1:
			kthpar[root][i] = kthpar[kthpar[root][i - 1]][i - 1]
	for child in gp[root]:
		if child == par:
			continue
		kthpar[child][0] = root
		dfs(child, root)
d = {')': '(', ']': '[', '}': '{', '>': '<'}
op = ['(', '[', '{', '<']

def hnbhai():
	n = sb()
	f = [0] * (n + 1)
	a = sd()
	for i in range(n):
		f[a[i]] = i
	tot = 0
	for i in range(2, n + 1):
		tot += abs(f[i] - f[i - 1])
	print(tot)
for _ in range(1):
	hnbhai()


n = int(input())
a = list(map(int, input().split()))
x = {}
for (idx, val) in enumerate(a):
	x[val] = idx
y = 0
for n in range(2, n + 1):
	y = y + abs(x[n] - x[n - 1])
print(y)


import sys
n = int(sys.stdin.readline())
l = list(map(int, sys.stdin.readline().split()))
k = 0
q = 0
z = [0] * n
for i in range(n):
	z[l[i] - 1] = i
ans = 0
while k < n - 1:
	ans += abs(z[k] - z[k + 1])
	k += 1
print(ans)


n = int(input())
data = list(map(int, input().split()))
for i in range(n):
	data[i] = [data[i], i + 1]
data.sort()
ans = 0
for i in range(1, n):
	ans += abs(data[i][1] - data[i - 1][1])
print(ans)


n = int(input())
s = {}
for (a, b) in enumerate(map(int, input().split())):
	s[b] = a
print(sum([abs(s[i] - s[i + 1]) for i in range(1, n)]))


try:
	t = int(input())
	a = list(map(int, input().split(' ')))
	c = [0] * (t + 1)
	for i in range(0, len(a)):
		c[a[i]] = i
	k = 0
	for i in range(1, len(c) - 1):
		k += abs(c[i] - c[i + 1])
	print(k)
except:
	pass


n = int(input())
d = {}
for (i, f) in enumerate(map(int, input().split())):
	d[f] = i
print(sum((abs(d[i] - d[i + 1]) for i in range(1, n))))


fragment_count = int(input())
line = input()
fragments = []
fragments = [int(i) for i in line.split()]
places = [0 for i in range(fragment_count)]
for (index, frag) in enumerate(fragments):
	places[frag - 1] = index
total_sum = 0
index = 0
for i in places:
	if index == len(places) - 1:
		break
	total_sum = total_sum + abs(i - places[index + 1])
	index = index + 1
print(total_sum)


import sys
from math import *

def minp():
	return sys.stdin.readline().strip()

def mint():
	return int(minp())

def mints():
	return map(int, minp().split())
n = mint()
a = [0] * n
j = 0
for i in mints():
	a[i - 1] = j
	j += 1
r = 0
for i in range(1, n):
	r += abs(a[i] - a[i - 1])
print(r)


n = int(input())
A = [int(i) for i in input().split()]
fin_list = list(enumerate(A))
fin_list.sort(key=lambda x: x[1])
tot_sum = 0
for i in range(n - 1):
	val = abs(fin_list[i + 1][0] - fin_list[i][0])
	tot_sum += val
print(tot_sum)


n = int(input())
l = [int(i) for i in input().split()]
ind = {}
for i in range(n):
	ind[l[i]] = i + 1
sm = 0
for i in range(2, n + 1):
	sm += abs(ind[i] - ind[i - 1])
print(sm)


n = int(input())
lst = list(map(int, input().split()))
(d, res) = ({}, 0)
for (i, x) in enumerate(lst):
	d[x] = i
k = d[1]
for i in range(1, n + 1):
	res += abs(d[i] - k)
	k = d[i]
print(res)


n = int(input())
x = list(map(int, input().split()))
for i in range(n):
	x[i] = [x[i]] + [i]
x.sort()
res = 0
for i in range(n - 1):
	res += abs(x[i][1] - x[i + 1][1])
print(res)


n = int(input())
lst = list(map(int, input().split()))
d = []
for i in range(n):
	d.append((lst[i], i))
d.sort()
s = 0
for i in range(1, n):
	s += abs(d[i][1] - d[i - 1][1])
print(s)


import sys
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
ans = 0
counter = Counter()
for (i, x) in enumerate(a):
	if x - 1 in counter:
		ans += i - counter[x - 1]
	if x + 1 in counter:
		ans += i - counter[x + 1]
	counter[x] = i
print(ans)


n = int(input())
a = list(map(int, input().split()))
ans = 0
li = {}
for i in range(n):
	li[a[i]] = i + 1
b = list(li.keys())
b.sort()
fi = li[b[0]]
for i in b[1:len(b)]:
	diff = li[i] - fi
	if diff < 0:
		diff = -diff
	else:
		pass
	ans += diff
	fi = li[i]
print(ans)


n = int(input())
ar = list(map(int, input().split()))
l = []
for i in range(n):
	l.append((ar[i], i + 1))
l.sort()
s = 0
for x in range(n - 1):
	s += abs(l[x + 1][1] - l[x][1])
print(s)


from operator import itemgetter
from itertools import islice
n = int(input())
a = map(int, input().split())

def main(a):
	hdd = sorted(enumerate(a), key=itemgetter(1))
	sectors = [i for (i, _) in hdd]
	return sum((abs(a - b) for (a, b) in zip(sectors, islice(sectors, 1, len(sectors)))))
print(main(a))


n = int(input())
s = input()
s = s.split()
d = {}
for i in range(len(s)):
	d[int(s[i]) - 1] = i
su = 0
for i in range(1, n):
	su += abs(d[i - 1] - d[i])
print(su)


k = 0
d = [0] * 200005
x = int(input())
p = list(map(int, input().split(' ')))
for i in range(len(p)):
	d[p[i]] = i
for i in range(1, x):
	k += abs(d[i + 1] - d[i])
print(k)


n = int(input())
a = [int(i) - 1 for i in input().split()]
b = [0] * n
for i in range(n):
	b[a[i]] = i
print(sum([abs(b[i] - b[i - 1]) for i in range(1, n)]))


import sys
fin = sys.stdin
n = int(fin.readline())
file = [None] * n
disk = tuple(map(int, input().split()))
for i in range(n):
	file[disk[i] - 1] = i
ctime = 0
for i in range(1, n):
	ctime += abs(file[i] - file[i - 1])
print(ctime)


n = int(input())
l = list(map(int, input().split()))
dp = {l[i]: i + 1 for i in range(n)}
s = 0
dp[0] = dp[1]
for i in range(1, n + 1):
	s = s + abs(dp[i] - dp[i - 1])
print(s)


n = int(input())
g = [-1] * (n + 1)
i = 1
for e in map(int, input().split()):
	g[e] = i
	i += 1
_sum = 0
for i in range(1, n):
	_sum += abs(g[i + 1] - g[i])
print(_sum)


n = int(input())
a = list(map(int, input().split()))
d = {}
for i in range(n):
	d[a[i]] = i + 1
s = 0
c = d[1]
for i in range(1, n):
	x = d[i + 1]
	s += abs(x - c)
	c = x
print(s)


import re
import sys
exit = sys.exit
from bisect import bisect_left as bsl, bisect_right as bsr
from collections import Counter, defaultdict as ddict, deque
from functools import lru_cache
cache = lru_cache(None)
from heapq import *
from itertools import *
from math import inf
from pprint import pprint as pp
enum = enumerate
ri = lambda : int(rln())
ris = lambda : list(map(int, rfs()))
rln = sys.stdin.readline
rl = lambda : rln().rstrip('\n')
rfs = lambda : rln().split()
cat = ''.join
catn = '\n'.join
mod = 1000000007
d4 = [(0, -1), (1, 0), (0, 1), (-1, 0)]
d8 = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]
n = ri()
a = ris()
b = [0] * n
for (i, x) in enum(a):
	b[x - 1] = i
ans = 0
for i in range(1, n):
	ans += abs(b[i - 1] - b[i])
print(ans)


(n, a, data, b, sum) = (int(input()), [], input(), 1, 0)
for i in range(n + 2):
	a.append(0)
for i in data.split():
	a[int(i)] = b
	b += 1
for i in range(2, n + 1):
	sum += abs(a[i - 1] - a[i])
print(sum)


n = int(input())
a = []
v = []
o = 0
a = list(map(int, input().split()))
for i in range(0, n + 1):
	v.append(0)
for i in range(n):
	v[a[i]] = i + 1
for i in range(1, n):
	o += abs(v[i] - v[i + 1])
print(o)


n = int(input())
f = list(map(int, input().split()))
t = [0] * n
ans = 0
for i in range(n):
	t[f[i] - 1] = i
for i in range(n - 1):
	ans += abs(t[i] - t[i + 1])
print(ans)


n = int(input())
L = [int(x) for x in input().split()]
pos = [0] * n
for i in range(n):
	pos[L[i] - 1] = i
ans = 0
for i in range(n - 1):
	ans += abs(pos[i] - pos[i + 1])
print(ans)


import bisect
from itertools import accumulate
import os
import sys
import math
from decimal import *
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def input():
	return sys.stdin.readline().rstrip('\r\n')

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True
n = int(input())
a = list(map(int, input().split()))
ans = []
for i in range(0, len(a)):
	ans.append([a[i], i])
ans.sort()
count = 0
for i in range(0, len(ans) - 1):
	count += abs(ans[i][1] - ans[i + 1][1])
print(count)


n = int(input())
f = list(map(int, input().split()))
t = []
for i in range(n):
	t.append((f[i], i))
t = sorted(t, key=lambda x: x[0])
ans = 0
for i in range(1, n):
	ans += abs(t[i][1] - t[i - 1][1])
print(ans)


(a, b, t) = (int(input()), list(map(int, input().split())), 0)
c = [0] * a
for i in range(a):
	c[b[i] - 1] = i
for i in range(a - 1):
	t += abs(c[i] - c[i + 1])
print(t)


import math
n = int(input())
f = [int(x) for x in input().split()]
l = list(sorted(enumerate(f), key=lambda x: x[1]))
s = 0
for i in range(1, n):
	s += abs(l[i][0] - l[i - 1][0])
print(s)


n = int(input())
index = [0 for i in range(n + 1)]
a = list(map(int, input().split()))
for i in range(n):
	index[a[i]] = i
ans = 0
lab = 1
while lab != n:
	ans += abs(index[lab] - index[lab + 1])
	lab += 1
print(ans)


n = int(input())
s = [(int(j), i) for (i, j) in enumerate(input().split())]
s.sort()
ans = 0
for i in range(1, n):
	ans += abs(s[i - 1][1] - s[i][1])
print(ans)


def main():
	n = int(input())
	l = list(map(int, input().split()))
	l = sorted(range(n), key=l.__getitem__)
	print(sum((abs(a - b) for (a, b) in zip(l, l[1:]))))
main()


(n, v) = (int(input()), list(map(int, input().split())))
id = [0] + [0 for _ in range(n)]
for (i, vi) in enumerate(v):
	id[vi] = i
print(sum((abs(id[i] - id[i - 1]) for i in range(2, len(id)))))


n = int(input())
a = list(map(int, input().split()))
ans = 0
b = [0 for i in range(n)]
for i in range(n):
	b[a[i] - 1] = i
for i in range(1, n):
	ans += abs(b[i] - b[i - 1])
print(ans)


import sys
from bisect import bisect_right as rb
from collections import deque
from queue import PriorityQueue
from math import *
input_ = lambda : sys.stdin.readline().strip('\r\n')
ii = lambda : int(input_())
il = lambda : list(map(int, input_().split()))
ilf = lambda : list(map(float, input_().split()))
ip = lambda : input_()
fi = lambda : float(input_())
ap = lambda ab, bc, cd: ab[bc].append(cd)
li = lambda : list(input_())
pr = lambda x: print(x)
prinT = lambda x: print(x)
f = lambda : sys.stdout.flush()
mod = 10 ** 9 + 7
n = ii()
a = il()
b = []
ans = 0
for i in range(n):
	b.append([a[i], i])
b.sort()
for i in range(1, n):
	ans += abs(b[i][1] - b[i - 1][1])
print(ans)


from collections import deque as de
import math
import re
from collections import Counter as cnt
from functools import reduce
from typing import MutableMapping
from itertools import groupby as gb
from fractions import Fraction as fr
from bisect import bisect_left as bl, bisect_right as br

def factors(n):
	return set(reduce(list.__add__, ([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))

class My_stack:

	def __init__(self):
		self.data = []

	def my_push(self, x):
		return self.data.append(x)

	def my_pop(self):
		return self.data.pop()

	def my_peak(self):
		return self.data[-1]

	def my_contains(self, x):
		return self.data.count(x)

	def my_show_all(self):
		return self.data

	def isEmpty(self):
		return len(self.data) == 0
arrStack = My_stack()

def decimalToBinary(n):
	return bin(n).replace('0b', '')

def binarytodecimal(n):
	return int(n, 2)

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	i = 5
	while i * i <= n:
		if n % i == 0 or n % (i + 2) == 0:
			return False
		i = i + 6
	return True

def get_prime_factors(number):
	prime_factors = []
	while number % 2 == 0:
		prime_factors.append(2)
		number = number / 2
	for i in range(3, int(math.sqrt(number)) + 1, 2):
		while number % i == 0:
			prime_factors.append(int(i))
			number = number / i
	if number > 2:
		prime_factors.append(int(number))
	return prime_factors

def get_frequency(list):
	dic = {}
	for ele in list:
		if ele in dic:
			dic[ele] += 1
		else:
			dic[ele] = 1
	return dic

def Log2(x):
	return math.log10(x) / math.log10(2)

def getProduct(n):
	product = 1
	while n != 0:
		product = product * (n % 10)
		n = n // 10
	return product

def dupconscount(nums):
	element = []
	freque = []
	if not nums:
		return element
	running_count = 1
	for i in range(len(nums) - 1):
		if nums[i] == nums[i + 1]:
			running_count += 1
		else:
			freque.append(running_count)
			element.append(nums[i])
			running_count = 1
	freque.append(running_count)
	element.append(nums[i + 1])
	return (element, freque)

def isPowerOfTwo(n):
	return math.ceil(Log2(n)) == math.floor(Log2(n))

def ceildiv(x, y):
	return (x + y - 1) // y

def di():
	return map(int, input().split())

def ii():
	return int(input())

def li():
	return sorted(list(map(int, input().split())))

def si():
	return list(map(str, input()))

def indic():
	dic = {}
	for (index, value) in enumerate(input().split()):
		dic[int(value)] = int(index) + 1
	return dic
n = ii()
c = indic()
ini = c[1]
ans = 0
for i in range(2, n + 1):
	ans += abs(ini - c[i])
	ini = c[i]
print(ans)


n = int(input())
thing = input().split(' ')
l = []
for i in range(n):
	l.append(int(thing[i]))
m = [-1] * n
for i in range(n):
	m[l[i] - 1] = i
t = 0
for i in range(n - 1):
	t += abs(m[i] - m[i + 1])
print(t)


n = int(input())
f = list(map(int, input().split()))
ff = [0] * n
for (i, e) in enumerate(f):
	ff[e - 1] = i
print(sum((abs(ff[i + 1] - ff[i]) for i in range(n - 1))))


def solve(fn):
	pos = {}
	for (i, e) in enumerate(fn):
		pos[e] = i
	res = 0
	for i in range(1, len(fn)):
		res += abs(pos[i] - pos[i + 1])
	return res
input()
print(solve(tuple(map(int, input().split()))))


n = int(input())
l = [int(i) for i in input().split(' ')]
new = [None] * n
for i in range(n):
	new[l[i] - 1] = i
s = 0
x = new[0]
for i in range(1, n):
	s += abs(x - new[i])
	x = new[i]
print(s)


n = int(input())
f = list(map(int, input().split()))
array = []
total = 0
for i in range(len(f)):
	array.append([f[i], i + 1])
array.sort()
for i in range(len(array) - 1):
	total += abs(array[i + 1][1] - array[i][1])
print(total)


n = int(input())
F = list(map(int, input().split()))
if n == 1:
	print(0)
	exit()
X = []
for (i, f) in enumerate(F):
	X.append((f, i))
X.sort()
ans = 0
for i in range(n - 1):
	ans += abs(X[i + 1][1] - X[i][1])
print(ans)


n = int(input())
a = list(map(int, input().split()))
lst = []
for i in range(n):
	lst.append([a[i], i])
lst.sort()
cnt = 0
for i in range(n - 1):
	cnt += abs(lst[i][1] - lst[i + 1][1])
print(cnt)


from _ast import arg, GtE
withFile = 0
if withFile == 1:
	fin = open('input.txt', 'r')
	fout = open('output.txt', 'w')

def getl():
	global withFile
	if withFile == 0:
		return input()
	else:
		return fin.readline()

def printl(s):
	global withFile
	if withFile == 0:
		print(s)
	else:
		fout.write(str(s))

def get_arr():
	global withFile
	x = getl().split(' ')
	if x[-1] == '':
		x.pop()
	return list(map(int, x))
n = get_arr()[0]
a = sorted([[x, i] for (i, x) in enumerate(get_arr())])
res = 0
for i in range(1, len(a)):
	res += abs(a[i][1] - a[i - 1][1])
print(res)
if withFile == 1:
	fin.close()
	fout.close()


n = int(input())
a = list(map(int, input().split()))
p = [0] * n
for i in range(n):
	p[a[i] - 1] = i + 1
s = 0
for i in range(n - 1):
	s += abs(p[i] - p[i + 1])
print(s)


a = int(input())
d = dict()
for (i, j) in enumerate(map(int, input().split())):
	d[j] = i
print(sum((abs(d[i] - d[i + 1]) for i in range(1, a))))


def solve():
	N = int(input())
	F = list(map(int, input().split()))
	G = []
	for i in range(N):
		G.append((F[i], i))
	G.sort()
	p = G[0][1]
	ans = 0
	for (f, np) in G:
		ans += abs(np - p)
		p = np
	print(ans)

def __starting_point():
	solve()
__starting_point()


n = int(input())
li = list(map(int, input().split()))
dict = {}
a = 1
for i in li:
	dict[i] = a
	a += 1
re = sorted(dict.items(), key=lambda x: x[0])
nw = [dict[i] for i in dict]
ans = 0
for i in range(1, len(nw)):
	ans += abs(nw[i] - nw[i - 1])
print(ans)


n = int(input())
our = list(map(int, input().split()))
place = dict()
for i in range(n):
	place[our[i]] = i
res = 0
for i in range(1, n):
	res += abs(place[i] - place[i + 1])
print(res)


import math
n = int(input())
lst = []
lst = [int(i) for i in input().split(' ')]
mp = {}
for i in range(0, len(lst)):
	mp[lst[i]] = i
pre = mp[1]
now = 0
ans = 0
for i in range(2, n + 1):
	now = mp[i]
	ans += abs(now - pre)
	pre = now
print(ans)


n = int(input())
pos = [0] * (n + 1)
for (idx, num) in enumerate(map(int, input().split())):
	pos[num] = idx
count = 0
for (a, b) in zip(pos[1:], pos[2:]):
	count += abs(a - b)
print(count)


(n, t) = (int(input()), 0)
x = sorted([(int(i), j) for (i, j) in zip(input().split(), range(n))], key=lambda x: x[0])
for i in range(n - 1):
	t += abs(x[i][1] - x[i + 1][1])
print(t)


n = int(input())
d = {}
for (i, x) in enumerate(input().split()):
	x = int(x)
	d[x] = i
res = 0
for i in range(2, n + 1):
	res += abs(d[i] - d[i - 1])
print(res)


n = int(input())
mas = list(map(int, input().split()))
mas1 = [0] * n
s = 0
for i in range(n):
	mas1[mas[i] - 1] = i
for i in range(1, n):
	s += abs(mas1[i - 1] - mas1[i])
print(s)


fragments = int(input())
dictionary = dict()
time = 0
l = input().split()
for i in range(len(l)):
	l[i] = int(l[i])
	dictionary[l[i]] = i + 1
array = sorted(dictionary.keys())
head = dictionary[array[0]]
for i in array:
	fragment = i
	time += abs(head - dictionary[i])
	head = dictionary[i]
print(time)


import math
import sys, string
input = sys.stdin.readline
import random
from collections import deque, defaultdict
import bisect
L = lambda : list(map(int, input().split()))
Ls = lambda : list(input().split())
M = lambda : map(int, input().split())
I = lambda : int(input())
d = defaultdict(int)
n = I()
l = L()
c = 0
for i in l:
	d[i] = c
	c += 1
p = d[1]
ans = 0
for i in range(2, n + 1):
	ans += abs(p - d[i])
	p = d[i]
print(ans)


n = int(input())
l = list(map(int, input().split()))
m = dict()
for (j, i) in enumerate(l):
	m[i] = j + 1
ans = 0
x = m[1]
for i in range(2, n + 1):
	ans += abs(m[i] - x)
	x = m[i]
print(ans)


from bisect import bisect
from itertools import permutations, combinations
n = int(input())
a = list(map(int, input().split()))
cnt = 1
b = []
for x in a:
	b.append([x, cnt])
	cnt += 1
b.sort()
res = 0
for i in range(1, n):
	res += abs(b[i][1] - b[i - 1][1])
print(res)


n = int(input())
f = input().split(' ')
pos = [0 for i in range(n)]
for i in range(n):
	pos[int(f[i]) - 1] = i
ret = 0
for i in range(n - 1):
	ret += abs(pos[i + 1] - pos[i])
print(ret)


n = int(input())
f = [int(x) for x in input().split()]
main = []
for i in range(n):
	main.append((f[i], i))
main.sort()
ans = 0
for i in range(n - 1):
	ans += abs(main[i][1] - main[i + 1][1])
print(ans)


n = int(input())
L = list(map(int, input().split()))
A = []
for i in range(n):
	A.append([L[i], i])
A.sort()
cnt = 0
for i in range(1, n):
	cnt += abs(A[i][1] - A[i - 1][1])
print(cnt)


input()
l = list(map(int, input().split()))
ans = 0
l2 = [0] * int(200000.0 + 5)
for i in range(len(l)):
	l2[l[i]] = i + 1
for i in range(1, len(l)):
	ans += abs(l2[i] - l2[i + 1])
print(ans)


def read_list():
	return [int(i) for i in input().split()]

def new_list(n):
	return [0 for i in range(n)]
n = int(input())
a = read_list()
pos = new_list(n)
for i in range(n):
	pos[a[i] - 1] = i
s = 0
for i in range(1, n):
	s += abs(pos[i] - pos[i - 1])
print(s)


n = int(input())
x = []
x = input().split()
s = {}
for i in range(n):
	s[int(x[i])] = i
ans = 0
for i in range(2, n + 1, 1):
	if s.get(i) > s.get(i - 1):
		ans = ans + (s.get(i) - s.get(i - 1))
	else:
		ans = ans + (s.get(i - 1) - s.get(i))
print(ans)


n = int(input())
lst = list(map(int, input().strip().split(' ')))
l = []
for j in range(n):
	l.append([lst[j], j])
l.sort(key=lambda x: x[0])
c = 0
for i in range(1, n):
	c += abs(l[i][1] - l[i - 1][1])
print(c)


n = int(input())
c = [0 for i in range(n)]
f = list(map(lambda x: int(x) - 1, input().split()))
for i in range(n):
	c[f[i]] = i
sum = 0
for i in range(n - 1):
	sum += abs(c[i] - c[i + 1])
print(sum)


def main():
	n = int(input())
	l = list(map(int, input().split()))
	d = {}
	for i in range(1, n + 1):
		d.update({l[i - 1]: i})
	last = -1
	answer = 0
	for element in d:
		if last == -1:
			last = d.get(element)
		else:
			el = d.get(element)
			answer += abs(last - el)
			last = el
	print(answer)
main()


n = int(input())
a = [0] * n
i = 1
c = 0
for item in input().split():
	a[int(item) - 1] = i
	i += 1
for i in range(n - 1):
	c += abs(a[i] - a[i + 1])
print(c)


import sys
if False:
	inp = open('A.txt', 'r')
else:
	inp = sys.stdin
n = int(inp.readline())
sectors = list(zip(map(int, inp.readline().split()), [x + 1 for x in range(n)]))
sectors.sort(key=lambda x: x[0])
ans = 0
for i in range(n - 1):
	ans += max(sectors[i][1] - sectors[i + 1][1], sectors[i + 1][1] - sectors[i][1])
print(ans)


n = int(input())
f = [int(s) for s in str(input()).split()]
sortedF = [None] * n
for i in range(n):
	sortedF[f[i] - 1] = i
moves = 0
for i in range(n - 1):
	moves += abs(sortedF[i + 1] - sortedF[i])
print(moves)


n = int(input())
a = list(map(int, input().strip().split()))
d = dict()
for i in range(n):
	d[a[i]] = i
s = 0
for i in range(1, n):
	s += abs(d[i + 1] - d[i])
print(s)


n = int(input())
arr = list(map(int, input().split()))
map = {}
for i in range(n):
	map[arr[i]] = i + 1
ans = 0
for i in range(1, n):
	ans += abs(map[i] - map[i + 1])
print(ans)


n = int(input())
fragments = tuple((int(x) for x in input().split()))

def solve(n, fragments):
	nfs = sorted([(fragments[n], n) for n in range(n)])
	result = sum((abs(nfs[i][1] - nfs[i - 1][1]) for i in range(1, n)))
	print(result)
solve(n, fragments)


n = input()
s = list(input().split())
i = 1
l = list()
for x in s:
	l.append((int(x), i))
	i += 1
l.sort()
ans = 0
for i in range(1, len(l)):
	ans += abs(l[i][1] - l[i - 1][1])
print(ans)


n = int(input())
L = list(map(int, input().split()))
M = [-1] * n
s = 0
for i in range(0, len(L)):
	M[L[i] - 1] = i
for i in range(1, len(M)):
	s += abs(M[i] - M[i - 1])
print(s)


input()
sum = 0
for (i, j) in sorted(list(enumerate(map(int, input().split()))), key=lambda elem: elem[1]):
	if j != 1:
		sum += abs(oldi - i)
	oldi = i
print(sum)


import collections
n = int(input())
l = [int(i) for i in input().split()]
d = {}
for i in range(n):
	d[l[i]] = i
d = dict(collections.OrderedDict(sorted(d.items())))
s = 0
for i in range(1, n):
	s += abs(d[i + 1] - d[i])
print(s)


import sys
import string
from collections import Counter, defaultdict
from math import fsum, sqrt, gcd, ceil, factorial
from operator import *
from itertools import accumulate
inf = float('inf')
flush = lambda : sys.stdout.flush
comb = lambda x, y: factorial(x) // factorial(y) // factorial(x - y)
en = lambda x: list(enumerate(x))
ip = lambda : input()
ii = lambda : int(input())
r = lambda : map(int, input().split())
rr = lambda : list(r())
n = ii()
arr = rr()
brr = defaultdict(lambda : 0)
for (i, j) in en(arr):
	brr[j] = i
x = brr[1]
c = 0
for i in range(2, n + 1):
	c += abs(brr[i] - x)
	x = brr[i]
print(c)


n = int(input())
fragments = [int(x) for x in input().split(' ')]
indexedFragments = {}
time = 0
indexes = []
for i in range(len(fragments)):
	indexedFragments[fragments[i]] = i
startPosIndex = indexedFragments[1]
for part in range(1, n + 1):
	nextPartIndex = indexedFragments[part]
	time += abs(startPosIndex - nextPartIndex)
	startPosIndex = nextPartIndex
print(time)


n = int(input())
dic = {}
for (i, x) in enumerate(map(int, input().split())):
	dic[x] = i
pos = dic[1]
c = 0
for i in range(2, n + 1):
	c += abs(dic[i] - pos)
	pos = dic[i]
print(c)


n = int(input())
m = input().split()
l = []
for i in range(n):
	l.append([int(m[i]), i])
l.sort()
step = 0
for i in range(1, n):
	step += abs(l[i][1] - l[i - 1][1])
print(step)


n = int(input())
fragments = list(map(int, input().split()))
fragmentsDict = {}
for i in range(n):
	fragmentsDict[fragments[i]] = i + 1
res = 0
for j in range(1, n):
	res += abs(fragmentsDict[j] - fragmentsDict[j + 1])
print(res)


n = int(input())
f = list(map(int, input().split()))
pos = [None] * n
for i in range(n):
	pos[f[i] - 1] = i
sum = 0
for i in range(n - 1):
	sum += abs(pos[i] - pos[i + 1])
print(sum)


from sys import argv, exit

def rstr():
	return input()

def rint():
	return int(input())

def rints():
	return [int(i) for i in input().split(' ')]

def prnt(*args):
	if '-v' in argv:
		print(*args)
n = rint()
f = rints()
d = {j: i + 1 for (i, j) in enumerate(f)}
sec = 1
total = 0
pos = d[sec]
while True:
	if sec == n:
		break
	sec += 1
	new_pos = d[sec]
	prnt('new', new_pos)
	total += abs(new_pos - pos)
	prnt('total', total)
	pos = new_pos
print(total)


from collections import *
from itertools import *
from random import *
from bisect import *
from string import *
from queue import *
from heapq import *
from math import *
from re import *
from sys import *

def fast():
	return stdin.readline().strip()

def zzz():
	return [int(i) for i in fast().split()]
(z, zz) = (input, lambda : list(map(int, z().split())))
(szz, graph, mod, szzz) = (lambda : sorted(zz()), {}, 10 ** 9 + 7, lambda : sorted(zzz()))

def lcd(xnum1, xnum2):
	return xnum1 * xnum2 // gcd(xnum1, xnum2)

def output(answer):
	stdout.write(str(answer))
dx = [-1, 1, 0, 0, 1, -1, 1, -1]
dy = [0, 0, 1, -1, 1, -1, -1, 1]
n = int(fast())
arr = zzz()
lst = []
for i in range(n):
	lst.append((arr[i], i))
lst = sorted(lst)
ans = 0
for i in range(n - 1):
	ans += abs(lst[i][1] - lst[i + 1][1])
print(ans)


n = int(input())
temp = input().split()
A = [0] * (n + 1)
sum = 0
for i in range(n):
	A[int(temp[i])] = i
for i in range(1, n):
	sum += abs(A[i] - A[i + 1])
print(sum)


n = int(input())
arr = list(map(int, input().split()))
idx = {}
for i in range(n):
	idx[arr[i]] = i + 1
sec = 0
for i in range(2, n + 1):
	sec += abs(idx[i] - idx[i - 1])
print(sec)


def main():
	n = int(input())
	fragments = input().split()
	fragments = [int(x) for x in fragments]
	array = [None] * (n + 1)
	for i in range(len(fragments)):
		array[fragments[i]] = i
	total = 0
	for i in range(1, len(array) - 1):
		total += abs(array[i + 1] - array[i])
	print(total)
main()


n = int(input())
k = 0
a = list(map(int, input().split()))
b = [0] * 1000000
for i in range(n):
	b[a[i] - 1] = i
for i in range(n - 1):
	k += abs(b[i] - b[i + 1])
print(k)


import sys
input = sys.stdin.readline

def main():
	n = int(input())
	l = [-1] + list(map(int, input().split()))
	fr = [0] * (n + 1)
	for i in range(1, n + 1):
		fr[l[i]] = i
	ans = 0
	for i in range(2, n + 1):
		ans += abs(fr[i] - fr[i - 1])
	print(ans)
main()


import sys
n = int(input())
a = list(map(int, input().split()))
a = list(enumerate(a))
for i in range(0, len(a)):
	a[i] = tuple(reversed(a[i]))
a.sort()
cur = a[0][1]
ans = 0
for i in range(1, len(a)):
	ans += abs(a[i][1] - cur)
	cur = a[i][1]
print(ans)


n = int(input())
ls = list(map(int, input().split()))
arr = [0] * n
for x in range(n):
	arr[ls[x] - 1] = x
su = 0
for x in range(1, n):
	su += abs(arr[x] - arr[x - 1])
print(su)


n = int(input())
f = [0] * n
x = list(map(int, input().split()))
for (i, e) in enumerate(x):
	f[e - 1] = i
ans = 0
for i in range(1, n):
	ans += abs(f[i] - f[i - 1])
print(ans)


R = lambda : map(int, input().split())
n = int(input())
v = list(R())
d = {}
for i in range(n):
	d[v[i]] = i
c = 0
for i in range(2, n + 1):
	c += abs(d[i] - d[i - 1])
print(c)


def solve(n: int, f: list):
	index_f = [(f[i], i) for i in range(n)]
	index_f.sort()
	answer = 0
	for i in range(n - 1):
		answer += abs(index_f[i][1] - index_f[i + 1][1])
	print(answer)
n = int(input())
f = list(map(int, input().split()))
solve(n, f)


n = input()
sectors = [int(x) for x in input().split()]

def my_arg_sort(list_of_sectors):
	new_list = [0] * len(list_of_sectors)
	for (i, elem) in enumerate(list_of_sectors):
		new_list[elem - 1] = i
	return new_list
sorted = my_arg_sort(sectors)
ans = 0
actual_position = sorted[0]
for k in sorted:
	ans += abs(k - actual_position)
	actual_position = k
print(ans)


from functools import cmp_to_key
from collections import deque, Counter
from heapq import heappush, heappop
from math import log, ceil
import sys
import os
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
if sys.version_info[0] < 3:
	(sys.stdin, sys.stdout) = (FastIO(sys.stdin), FastIO(sys.stdout))
else:
	(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))

def print(*args, **kwargs):
	(sep, file) = (kwargs.pop('sep', ' '), kwargs.pop('file', sys.stdout))
	at_start = True
	for x in args:
		if not at_start:
			file.write(sep)
		file.write(str(x))
		at_start = False
	file.write(kwargs.pop('end', '\n'))
	if kwargs.pop('flush', False):
		file.flush()

def inp():
	return sys.stdin.readline().rstrip('\r\n')

def ii():
	return int(inp())

def si():
	return str(inp())

def li(lag=0):
	l = list(map(int, inp().split()))
	if lag != 0:
		for i in range(len(l)):
			l[i] += lag
	return l

def mi(lag=0):
	matrix = list()
	for i in range(n):
		matrix.append(li(lag))
	return matrix

def lsi():
	return list(map(str, inp().split()))

def print_list(lista, space=' '):
	print(space.join(map(str, lista)))

def bisect_left(a, x):
	left = 0
	right = len(a)
	while left < right:
		mid = (left + right) // 2
		if a[mid] < x:
			left = mid + 1
		else:
			right = mid
	return left

def bisect_right(a, x):
	left = 0
	right = len(a)
	while left < right:
		mid = (left + right) // 2
		if a[mid] > x:
			right = mid
		else:
			left = mid + 1
	return left

def bisect_elements(a, x):
	return bisect_right(a, x) - bisect_left(a, x)
MOD = 10 ** 9 + 7
maxN = 5
FACT = [0] * maxN
INV_FACT = [0] * maxN

def add(x, y):
	return (x + y) % MOD

def multiply(x, y):
	return x * y % MOD

def power(x, y):
	if y == 0:
		return 1
	elif y % 2:
		return multiply(x, power(x, y - 1))
	else:
		a = power(x, y // 2)
		return multiply(a, a)

def inverse(x):
	return power(x, MOD - 2)

def divide(x, y):
	return multiply(x, inverse(y))

def allFactorials():
	FACT[0] = 1
	for i in range(1, maxN):
		FACT[i] = multiply(i, FACT[i - 1])

def inverseFactorials():
	n = len(INV_FACT)
	INV_FACT[n - 1] = inverse(FACT[n - 1])
	for i in range(n - 2, -1, -1):
		INV_FACT[i] = multiply(INV_FACT[i + 1], i + 1)

def coeffBinom(n, k):
	if n < k:
		return 0
	return multiply(FACT[n], multiply(INV_FACT[k], INV_FACT[n - k]))

def create_graph(n, m, undirected=1, unweighted=1):
	graph = [[] for i in range(n)]
	if unweighted:
		for i in range(m):
			[x, y] = li(lag=-1)
			graph[x].append(y)
			if undirected:
				graph[y].append(x)
	else:
		for i in range(m):
			[x, y, w] = li(lag=-1)
			w += 1
			graph[x].append([y, w])
			if undirected:
				graph[y].append([x, w])
	return graph

def create_tree(n, unweighted=1):
	children = [[] for i in range(n)]
	if unweighted:
		for i in range(n - 1):
			[x, y] = li(lag=-1)
			children[x].append(y)
			children[y].append(x)
	else:
		for i in range(n - 1):
			[x, y, w] = li(lag=-1)
			w += 1
			children[x].append([y, w])
			children[y].append([x, w])
	return children

def dist(tree, n, A, B=-1):
	s = [[A, 0]]
	(massimo, massimo_nodo) = (0, 0)
	distanza = -1
	v = [-1] * n
	while s:
		(el, dis) = s.pop()
		if dis > massimo:
			massimo = dis
			massimo_nodo = el
		if el == B:
			distanza = dis
		for child in tree[el]:
			if v[child] == -1:
				v[child] = 1
				s.append([child, dis + 1])
	return (massimo, massimo_nodo, distanza)

def diameter(tree):
	(_, foglia, _) = dist(tree, n, 0)
	(diam, _, _) = dist(tree, n, foglia)
	return diam

def dfs(graph, n, A):
	v = [-1] * n
	s = [[A, 0]]
	v[A] = 0
	while s:
		(el, dis) = s.pop()
		for child in graph[el]:
			if v[child] == -1:
				v[child] = dis + 1
				s.append([child, dis + 1])
	return v

def bfs(graph, n, A):
	v = [-1] * n
	s = deque()
	s.append([A, 0])
	v[A] = 0
	while s:
		(el, dis) = s.popleft()
		for child in graph[el]:
			if v[child] == -1:
				v[child] = dis + 1
				s.append([child, dis + 1])
	return v

def parents_children_root_unrooted_tree(tree, n, root=0):
	q = deque()
	visited = [0] * n
	parent = [-1] * n
	children = [[] for i in range(n)]
	q.append(root)
	while q:
		all_done = 1
		visited[q[0]] = 1
		for child in tree[q[0]]:
			if not visited[child]:
				all_done = 0
				q.appendleft(child)
		if all_done:
			for child in tree[q[0]]:
				if parent[child] == -1:
					parent[q[0]] = child
					children[child].append(q[0])
			q.popleft()
	return (parent, children)

def all_longest_path_passing_from_node(parent, children, n):
	q = deque()
	visited = [len(children[i]) for i in range(n)]
	downwards = [[0, 0] for i in range(n)]
	upward = [1] * n
	longest_path = [1] * n
	for i in range(n):
		if not visited[i]:
			q.append(i)
			downwards[i] = [1, 0]
	while q:
		node = q.popleft()
		if parent[node] != -1:
			visited[parent[node]] -= 1
			if not visited[parent[node]]:
				q.append(parent[node])
		else:
			root = node
		for child in children[node]:
			downwards[node] = sorted([downwards[node][0], downwards[node][1], downwards[child][0] + 1], reverse=True)[0:2]
	s = [node]
	while s:
		node = s.pop()
		if parent[node] != -1:
			if downwards[parent[node]][0] == downwards[node][0] + 1:
				upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][1])
			else:
				upward[node] = 1 + max(upward[parent[node]], downwards[parent[node]][0])
		longest_path[node] = downwards[node][0] + downwards[node][1] + upward[node] - min([downwards[node][0], downwards[node][1], upward[node]]) - 1
		for child in children[node]:
			s.append(child)
	return longest_path
n = ii()
a = li()
pos = [-1 for i in range(n)]
for i in range(n):
	el = a[i] - 1
	pos[el] = i
s = 0
for i in range(1, n):
	s += abs(pos[i] - pos[i - 1])
print(s)


n = int(input())
location = {int(val): i for (i, val) in enumerate(input().split())}
result = sum((abs(location[i] - location[i - 1]) for i in range(2, n + 1)))
print(result)


def main():
	buf = input()
	n = int(buf)
	buf = input()
	buflist = buf.split()
	f = list(map(int, buflist))
	f = list(map(lambda x: x - 1, f))
	s = []
	for i in range(n):
		s.append(i)
	s = [z for (_, z) in sorted(zip(f, s), key=lambda pair: pair[0])]
	f = list(sorted(f))
	t = 0
	for i in range(len(s) - 1):
		t += abs(s[i] - s[i + 1])
	print(t)
main()


import os

def abs(a):
	if a > 0:
		return a
	else:
		return -a

def main():
	n = int(input())
	a = list(map(int, input().split()))
	b = [0] * n
	for i in range(n):
		b[a[i] - 1] = i
	res = 0
	for i in range(1, n):
		res += abs(b[i] - b[i - 1])
	print(res)
main()


N = int(input())
a = list(map(int, input().split()))
x = {}
for i in range(N):
	x[a[i]] = i + 1
print(sum((abs(x[i] - x[i - 1]) for i in range(2, N + 1))))


def hdd(n, lst):
	(res, answer) = ([0] * n, 0)
	for i in range(n):
		res[lst[i] - 1] = i
	for i in range(n - 1):
		answer += abs(res[i] - res[i + 1])
	return answer
m = int(input())
a = [int(j) for j in input().split()]
print(hdd(m, a))


n = int(input())
f = list(map(int, input().split()))
dmass = []
for i in range(n):
	dmass += [(f[i], i)]
dmass.sort()
ans = 0
for i in range(1, n):
	ans += abs(dmass[i][1] - dmass[i - 1][1])
print(ans)


sz = int(input())
pos = [None] * sz
for (p, i) in enumerate(map(int, input().split())):
	pos[i - 1] = p
last = pos[0]
total = 0
for i in range(sz):
	total += abs(last - pos[i])
	last = pos[i]
print(total)


n = int(input())
arr = list(map(int, input().split()))
map = dict()
for (idx, v) in enumerate(arr):
	map[v] = idx + 1
ans = 0
for i in range(2, n + 1):
	ans += abs(map[i] - map[i - 1])
print(ans)


n = int(input())
a = list(map(int, input().split()))
dict = {}
for (i, x) in enumerate(a):
	dict[x - 1] = i
print(sum((abs(dict[i] - dict[i - 1]) for i in range(1, n))))


(n, s) = (int(input()), 0)
ar = [(int(value), index + 1) for (index, value) in enumerate(input().split())]
ar.sort()
for x in range(n - 1):
	s += abs(ar[x + 1][1] - ar[x][1])
print(s)


N = int(input())
t = input().strip().split(' ')
t = [int(i) for i in t]
fragmentToSector = [0] * N
for i in range(0, N):
	fragment = t[i]
	fragmentToSector[fragment - 1] = i + 1
total = 0
for i in range(1, N):
	total += abs(fragmentToSector[i] - fragmentToSector[i - 1])
print(total)