#include <bits/stdc++.h>   using namespace std;   const int inf = 1e9 + 1;   const int N = 100123;   int z, n;   int a[N];   int best[N];   long long ans[N];   long long res;   pair<int, int> tree[N << 2];   void build(int v, int l, int r) {     if (l == r) {       tree[v] = {a[l], l};       return;     }     int mid = (l + r) >> 1;     build(2 * v, l, mid);     build(2 * v + 1, mid + 1, r);     tree[v] = max(tree[2 * v], tree[2 * v + 1]);   }   pair<int, int> query(int v, int l, int r, int ql, int qr) {     if (ql <= l && qr >= r) {       return tree[v];     }     int mid = (l + r) >> 1;     pair<int, int> res = {0, 0};     if (ql <= mid) res = max(res, query(2 * v, l, mid, ql, qr));     if (qr >= mid + 1) res = max(res, query(2 * v + 1, mid + 1, r, ql, qr));     return res;   }   int main() {     ios_base::sync_with_stdio(0);     cin >> n;     for (int i = 1; i <= n - 1; i++) {       cin >> a[i];     }     a[n] = n;     build(1, 1, n);     for (int i = 1; i <= n; i++) {       pair<int, int> b = query(1, 1, n, i, a[i]);       best[i] = b.second;     }     ans[n - 1] = 1;     res++;     for (int i = n - 2; i >= 1; i--) {       ans[i] = ans[best[i]] + (best[i] - i) + (n - a[i]);       res += ans[i];     }     cout << res << endl;   }   