Vasya commutes by train every day. There are n train stations in the city, and at the i-th station it's possible to buy only tickets to stations from i + 1 to a_{i} inclusive. No tickets are sold at the last station.

Let ρ_{i}, j be the minimum number of tickets one needs to buy in order to get from stations i to station j. As Vasya is fond of different useless statistic he asks you to compute the sum of all values ρ_{i}, j among all pairs 1 ≤ i < j ≤ n.


-----Input-----

The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of stations.

The second line contains n - 1 integer a_{i} (i + 1 ≤ a_{i} ≤ n), the i-th of them means that at the i-th station one may buy tickets to each station from i + 1 to a_{i} inclusive.


-----Output-----

Print the sum of ρ_{i}, j among all pairs of 1 ≤ i < j ≤ n.


-----Examples-----
Input
4
4 4 4

Output
6

Input
5
2 3 5 5

Output
17



-----Note-----

In the first sample it's possible to get from any station to any other (with greater index) using only one ticket. The total number of pairs is 6, so the answer is also 6.

Consider the second sample:   ρ_{1, 2} = 1  ρ_{1, 3} = 2  ρ_{1, 4} = 3  ρ_{1, 5} = 3  ρ_{2, 3} = 1  ρ_{2, 4} = 2  ρ_{2, 5} = 2  ρ_{3, 4} = 1  ρ_{3, 5} = 1  ρ_{4, 5} = 1 

Thus the answer equals 1 + 2 + 3 + 3 + 1 + 2 + 2 + 1 + 1 + 1 = 17.

Difficulty: HARD

Solutions:
import sys
input = sys.stdin.readline

def solve():
	n = int(input())
	a = list(map(int, input().split()))
	T = [(-1, -1)] * (2 * n)
	for i in range(n - 1):
		T[i + n] = (a[i], i)
	T[n + n - 1] = (n, n - 1)
	for i in range(n - 1, -1, -1):
		T[i] = max(T[2 * i], T[2 * i + 1])
	dp = [0] * n
	res = 0
	for i in range(n - 2, -1, -1):
		l = i + n
		r = a[i] - 1 + n
		v = (-1, -1)
		while l <= r:
			if l % 2 == 1:
				v = max(v, T[l])
			if r % 2 == 0:
				v = max(v, T[r])
			l = (l + 1) // 2
			r = (r - 1) // 2
		dp[i] = dp[v[1]] + (n - v[1]) + (v[1] - i) - (a[i] - 1 - v[1] + 1)
		res += dp[i]
	print(res)
solve()


from sys import stdin
MAXN = 1 << 17

def build(arr, n, segtree):
	for i in range(n):
		segtree[MAXN + i] = [arr[i], i]
	for i in range(MAXN - 1, 0, -1):
		segtree[i] = max(segtree[i * 2], segtree[i * 2 + 1])

def get(l, r, segtree):
	ans = [-1, -1]
	(l, r) = (l + MAXN, r + MAXN + 1)
	while l < r:
		if l & 1:
			ans = max(ans, segtree[l])
			l += 1
		if r & 1:
			r -= 1
			ans = max(ans, segtree[r])
		l >>= 1
		r >>= 1
	return ans[1]
n = int(input().strip())
arr = list(map(int, stdin.readline().strip().split(' ')))
arr = arr + [n]
for i in range(n):
	arr[i] -= 1
segtree = [[0, 0] for i in range(4 * MAXN)]
build(arr, n, segtree)
dp = [0 for i in range(n)]
ans = 0
for i in range(n - 2, -1, -1):
	m = get(i + 1, arr[i], segtree)
	dp[i] = dp[m] - (arr[i] - m) + n - i - 1
	ans += dp[i]
print(ans)


def main():
	from collections import deque
	n = int(input())
	(aa, bb) = (list(map(int, input().split())), [0] * n)
	(q, bb[1]) = (deque([(n - 1, aa.pop())]), 1)
	aa.reverse()
	for (i, a) in enumerate(aa, 2):
		if a == n:
			bb[i] = i
		else:
			(lo, hi) = (0, len(q))
			while lo <= hi:
				mid = (lo + hi) // 2
				if q[mid][0] > a:
					lo = mid + 1
				else:
					hi = mid - 1
			x = q[lo][0]
			bb[i] = x + i + bb[n - x] - a
		while q and q[-1][1] < a:
			del q[-1]
		q.append((n - i, a))
	print(sum(bb))

def __starting_point():
	main()
__starting_point()


from collections import deque

def argmax(que, z):
	l = 0
	r = len(que) - 1
	while l <= r:
		mid = int((l + r) / 2)
		x = que[mid]['i']
		if x <= z:
			r = mid - 1
		else:
			l = mid + 1
	return que[l]['i']

def solve(n, A):
	a = [0] * (n + 1)
	a[1:] = A
	dp = [0] * (n + 1)
	dp[n - 1] = 1
	que = deque()
	que.append({'i': n - 1, 'a': a[n - 1]})
	for i in range(n - 2, 0, -1):
		if a[i] >= n:
			dp[i] = n - i
		else:
			x = argmax(que, a[i])
			dp[i] = x - i + dp[x] + n - a[i]
		while len(que) > 0 and que[-1]['a'] < a[i]:
			que.pop()
		que.append({'i': i, 'a': a[i]})
	return sum(dp)
n = int(input())
a = list(map(int, input().split(' ')))
print(solve(n, a))


import math
n = int(input())
s = [int(x) for x in input().split()] + [n]
K = int(math.log2(n)) + 1
lookup = []
for i in range(n + 1):
	h = []
	for j in range(K + 1):
		h.append(0)
	lookup.append(h)
for i in range(0, n):
	lookup[i][0] = (s[i], i)
for j in range(1, K + 1):
	i = 0
	while i + (1 << j) - 1 < n:
		if lookup[i][j - 1][0] > lookup[i + (1 << j - 1)][j - 1][0]:
			lookup[i][j] = (lookup[i][j - 1][0], lookup[i][j - 1][1])
		else:
			lookup[i][j] = (lookup[i + (1 << j - 1)][j - 1][0], lookup[i + (1 << j - 1)][j - 1][1])
		i += 1
	j += 1
pos = []
for i in range(0, len(s) - 1):
	L = i + 1
	R = s[i] - 1
	j = int(math.log2(R - L + 1))
	if lookup[L][j][0] > lookup[R - (1 << j) + 1][j][0]:
		pos.append(lookup[L][j][1])
	else:
		pos.append(lookup[R - (1 << j) + 1][j][1])
ans = 0
dp = [0] * n
for i in range(n - 2, -1, -1):
	dp[i] = n - i + dp[pos[i]] - (s[i] - pos[i])
	ans += dp[i]
print(ans)


MAX_N = 100000

def maxi(a, b):
	if a[0] > b[0]:
		return a
	else:
		return b

class Segment_Tree:

	def init(self, left, right, data, leftbound, rightbound):
		self.data = data
		self.left = left
		self.right = right
		self.leftbound = leftbound
		self.rightbound = rightbound
		return self

	def build(self, a, leftbound, rightbound):
		if len(a) == 0:
			return self.init(-1, -1, [0, -1], MAX_N + 1, -1)
		elif len(a) == 1:
			return self.init(-1, -1, a[0], leftbound, rightbound)
		else:
			middle = (leftbound + rightbound) // 2
			self.left = Segment_Tree()
			self.right = Segment_Tree()
			return self.init(self.left.build(a[:middle - leftbound], leftbound, middle), self.right.build(a[middle - leftbound:], middle, rightbound), maxi(self.left.data, self.right.data), leftbound, rightbound)

	def get(self, l, r):
		if l <= self.leftbound and r >= self.rightbound:
			return self.data
		elif l < self.left.rightbound and r > self.right.leftbound:
			return maxi(self.left.get(l, r), self.right.get(l, r))
		elif l >= self.right.leftbound:
			return self.right.get(l, r)
		else:
			return self.left.get(l, r)
n = int(input())
a = list(map(int, input().split())) + [n]
a = [[a[i] - 1, i] for i in range(n)]
Tree = Segment_Tree()
Tree.build(a, 0, n)
dp = [0] * n
ans = 0
for i in range(n - 2, -1, -1):
	m = Tree.get(i + 1, a[i][0] + 1)[1]
	dp[i] = dp[m] - (a[i][0] - m) + n - i - 1
	ans += dp[i]
print(ans)


n = int(input())
a = list(map(int, input().split()))
a = [ai - 1 for ai in a]
a[n:n] = [n - 1]
dp = [0] * n
ans = 0
i = n - 2
nmax = 2 ** 17
tree = [[0, 0]] * 2 * nmax
j = 0
while j < n:
	tree[nmax + j] = [a[j], j]
	j = j + 1
j = nmax - 1
while j > 0:
	tree[j] = max(tree[j * 2], tree[j * 2 + 1])
	j = j - 1

def get(left, right):
	ans = [-1, -1]
	left = left + nmax
	right = right + nmax + 1
	while left < right:
		if left & 1:
			ans = max(ans, tree[left])
			left = left + 1
		if right & 1:
			right = right - 1
			ans = max(ans, tree[right])
		left = left // 2
		right = right // 2
	return ans[1]
while i >= 0:
	m = get(i + 1, a[i])
	dp[i] = dp[m] - (a[i] - m) + n - i - 1
	ans += dp[i]
	i = i - 1
print(ans)


import os, sys
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))
input = lambda : sys.stdin.readline().rstrip('\r\n')
mod = 998244353
from math import log2
from bisect import bisect_right
from math import log2

def sparse_table(ar):
	n = len(ar)
	t = int(log2(n)) + 1
	sp = [[[0, 0] for _ in range(t)] for _ in range(n)]
	for i in range(n):
		sp[i][0][0] = ar[i]
		sp[i][0][1] = i
	for j in range(1, t):
		for i in range(n - 2 ** j + 1):
			(v1, i1) = sp[i][j - 1]
			(v2, i2) = sp[i + 2 ** (j - 1)][j - 1]
			if v2 > v1:
				sp[i][j][0] = v2
				sp[i][j][1] = i2
			else:
				sp[i][j][0] = v1
				sp[i][j][1] = i1
	return sp

def solve(i, j):
	t = int(log2(j - i + 1))
	j = j - 2 ** t + 1
	(v1, i1) = sp[i][t]
	(v2, i2) = sp[j][t]
	if v2 > v1:
		return i2
	return i1
n = int(input())
a = list(map(int, input().split()))
sp = sparse_table(a)
dp = [0] * n
dp[n - 2] = 1
for i in range(n - 3, -1, -1):
	if a[i] == n:
		t = solve(i + 1, a[i] - 2)
	else:
		t = solve(i + 1, a[i] - 1)
	dp[i] = dp[t] + (a[i] - 1 - i) - (a[i] - 1 - t) + (n - 1 - (a[i] - 1))
print(sum(dp))