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: interview

Starter code:

Solutions:
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):
        #print(leftbound, rightbound, a)
        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)


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()

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;

#Build Segment tree
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
    
#get max of a interval [left, right]
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)

from collections import deque


def argmax(que, z): #二分求[i+1, a[i]]中a[x]最大的x
    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))


"""
n=int(input())
s=[int(x) for x in input().split()]+[n]
pos=[]
for i in range(0,len(s)-1):
    temp=-1
    p=i+1
    for j in range(i+1,s[i]):
        if(temp<s[j]):
            temp=max(temp,s[j])
            p=j
    pos.append(j)

pos.append(0)

ans=0
dp=[0]*n
for i in range(n-2,-1,-1):
    dp[i]=(n-i-1)+(dp[pos[i]])-(s[i]-pos[i]-1)
    ans+=dp[i]
print(pos)
print(dp)
print(ans)
"""

"""
n=int(input())
s=[1]+[int(x) for x in input().split()]+[n]
pos=[0]
for i in range(1,len(s)-1):
    temp=-1
    p=i+1
    for j in range(i+1,s[i]+1):
        if(temp<s[j]):
            temp=max(temp,s[j])
            p=j
    pos.append(p)

pos.append(0)

ans=0
dp=[0]*(n+1)
for i in range(n-1,0,-1):
    dp[i]=(n-i)+(dp[pos[i]])-(s[i]-pos[i])
    ans+=dp[i]

#print(s)
#print(pos)
#print(dp)
print(ans)



n=int(input())
s=[int(x) for x in input().split()]+[n]
pos=[]
for i in range(0,len(s)-1):
    temp=-1
    p=i+1
    for j in range(i+1,s[i]):
        if(temp<s[j]):
            temp=max(temp,s[j])
            p=j
    pos.append(p)

pos.append(0)

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(s)
#print(pos)
#print(dp)
print(ans)



"""

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):
#j=1
#while((1<<j)<=n):
    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


#print(lookup)        

"""pos=[]
for i in range(0,len(s)-1):
    temp=-1
    p=i+1
    for j in range(i+1,s[i]):
        if(temp<s[j]):
            temp=max(temp,s[j])
            p=j
    pos.append(p)

pos.append(0)"""

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(s)
#print(pos)
#print(dp)
print(ans)




"""
n=int(input())
s=[int(x) for x in input().split()]+[n]
pos=[]
for i in range(0,len(s)-1):
    temp=-1
    p=i+1
    for j in range(i+1,s[i]):
        if(temp<s[j]):
            temp=max(temp,s[j])
            p=j
    pos.append(p)

pos.append(0)

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(s)
#print(pos)
#print(dp)
print(ans)



"""

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):
#j=1
#while((1<<j)<=n):
    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)