#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define f first
#define s second
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}

const int MAXN = 1e5 + 1, BLOCK = 300, CBLOCKS = MAXN / BLOCK + 10;
ll a[MAXN];
ll lb[CBLOCKS], rb[CBLOCKS], numblock[MAXN], mblock[CBLOCKS];
ll sumdist[CBLOCKS][BLOCK];
pair<ll, ll> blockdist[CBLOCKS][BLOCK];
ll n, cblock;

void init() {
    lb[0] = 0;
    cblock = 0;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        ++cur;
        numblock[i] = cblock;
        chkmax(mblock[cblock], a[i]);
        if (cur == BLOCK) {
            rb[cblock++] = i;
            cur = 0;
            lb[cblock] = i + 1;
        }
    }
    if (cur) rb[cblock++] = n - 1;
    for (int cur = 0; cur < cblock; ++cur) {
        int mx1 = 0;
        for (int i = lb[cur]; i <= rb[cur]; ++i) {
            chkmax(mx1, a[i]);
            int cdist = 1, covered = a[i], mx = mx1;
            for (int j = i + 1; j <= rb[cur]; ++j) {
                if (j > covered) {
                    ++cdist;
                    covered = mx;
                }
                sumdist[cur][i - lb[cur]] += cdist;
                chkmax(mx, a[j]);
            }
            blockdist[cur][i - lb[cur]] = {cdist, covered};
        }
    }
}

ll get(int num) {
    ll res = 0;
    int cur = numblock[num];
    // counting for this block
    int cdist = 1, covered = a[num], mx = a[num];
    for (int j = num + 1; j <= rb[cur]; ++j) {
        if (j > covered) {
            ++cdist;
            covered = mx;
        }
        res += cdist;
        chkmax(mx, a[j]);
    }
    if (covered <= rb[cur]) {
        ++cdist;
        covered = mx;
    }
    // cout << num << ' ' << res << endl;
    for (++cur; cur < cblock; ++cur) {
        // cout << '!' << cur << ' ' << cdist << endl;
        res += 1ll * (cdist - 1) * (rb[cur] - lb[cur] + 1);
        if (rb[cur] > covered) {
            res += sumdist[cur][covered - lb[cur]];
            cdist += blockdist[cur][covered - lb[cur]].f;
            covered = blockdist[cur][covered - lb[cur]].s;
        }
        else
            res += rb[cur] - lb[cur] + 1;
        chkmax(mx, mblock[cur]);
        if (covered <= rb[cur]) {
            ++cdist;
            covered = mx;
        }
    }
    // cout << num << ' ' << res << endl << endl;
    return res;
}

int main() {
    IO;
    // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
        cin >> a[i], --a[i];
    a[n - 1] = n - 1;
    init();
    // for (int i = 0; i < n; ++i)
    //     cout << sumdist[numblock[i]][i - lb[numblock[i]]] << ' ';
    ll ans = 0;
    for (int i = 0; i < n; ++i) ans += get(i);
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define f first
#define s second
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}

const int MAXN = 1e5 + 1, BLOCK = 300, CBLOCKS = MAXN / BLOCK + 10;
ll a[MAXN];
ll lb[CBLOCKS], rb[CBLOCKS], numblock[MAXN], mblock[CBLOCKS][BLOCK];
ll sumdist[CBLOCKS][BLOCK];
pair<ll, ll> blockdist[CBLOCKS][BLOCK];
ll n, cblock;

void init() {
    lb[0] = 0;
    cblock = 0;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        ++cur;
        numblock[i] = cblock;
        if (cur > 1)
            mblock[cblock][cur - 1] = mblock[cblock][cur - 2];
        chkmax(mblock[cblock][cur - 1], a[i]);
        if (cur == BLOCK) {
            rb[cblock++] = i;
            cur = 0;
            lb[cblock] = i + 1;
        }
    }
    if (cur) rb[cblock++] = n - 1;
    for (int cur = 0; cur < cblock; ++cur) {
        for (int i = lb[cur]; i <= rb[cur]; ++i) {
            int cdist = 1, covered = mblock[cur][i - lb[cur]], mx = covered;
            for (int j = i + 1; j <= rb[cur]; ++j) {
                if (j > covered) {
                    ++cdist;
                    covered = mx;
                }
                sumdist[cur][i - lb[cur]] += cdist;
                chkmax(mx, a[j]);
            }
            blockdist[cur][i - lb[cur]] = {cdist, covered};
        }
    }
}

ll get(int num) {
    ll res = 0;
    int cur = numblock[num];
    // counting for this block
    ll cdist = 1, covered = a[num], mx = a[num];
    for (int j = num + 1; j <= rb[cur]; ++j) {
        if (j > covered) {
            ++cdist;
            covered = mx;
        }
        res += cdist;
        chkmax(mx, a[j]);
    }
    if (covered < rb[cur]) cout << 1 / 0;
    if (covered == rb[cur]) {
        ++cdist;
        covered = mx;
    }
    // cout << num << ' ' << res << endl;
    for (++cur; cur < cblock; ++cur) {
        // cout << '!' << cur << ' ' << cdist << endl;
        res += 1ll * cdist * (rb[cur] - lb[cur] + 1);
        if (rb[cur] > covered) {
            if (mblock[cur][covered - lb[cur]] >= mx) {
                res += sumdist[cur][covered - lb[cur]];
                cdist += blockdist[cur][covered - lb[cur]].f;
                covered = blockdist[cur][covered - lb[cur]].s;
            }
            else {
                ++cdist;
                res += min(rb[cur], 1ll * mx) - covered;
                covered = mx;
                if (rb[cur] > covered) {
                    res += sumdist[cur][covered - lb[cur]];
                    cdist += blockdist[cur][covered - lb[cur]].f;
                    covered = blockdist[cur][covered - lb[cur]].s;
                }
            }
        }
        chkmax(mx, mblock[cur][rb[cur] - lb[cur]]);
        if (covered < rb[cur]) cout << 1 / 0;
        if (covered == rb[cur]) {
            ++cdist;
            covered = mx;
        }
    }
    // cout << num << ' ' << res << endl << endl;
    return res;
}

ll solve_stupid(int n) {
    ll res = 0;
    for (int i = 0; i < n; ++i) {
        int mx = a[i], covered = a[i], dist = 1;
        for (int j = i + 1; j < n; ++j) {
            if (j > covered) {
                ++dist;
                covered = mx;
            }
            res += dist;
            chkmax(mx, a[j]);
        }
    }
    return res;
}

int main() {
    IO;
    // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
        cin >> a[i], --a[i];
    a[n - 1] = n - 1;
    // init();
    // for (int i = 0; i < n; ++i)
    //     cout << sumdist[numblock[i]][i - lb[numblock[i]]] << ' ';
    // ll ans = 0;
    // for (int i = 0; i < n; ++i) ans += get(i);
    cout << solve_stupid(n);
    return 0;
}


#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define f first
#define s second
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}

const int MAXN = 1e5 + 1, BLOCK = 300, CBLOCKS = MAXN / BLOCK + 10;
ll a[MAXN];
ll lb[CBLOCKS], rb[CBLOCKS], numblock[MAXN], mblock[CBLOCKS][BLOCK];
ll sumdist[CBLOCKS][BLOCK];
pair<ll, ll> blockdist[CBLOCKS][BLOCK];
ll n, cblock;

void init() {
    lb[0] = 0;
    cblock = 0;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        ++cur;
        numblock[i] = cblock;
        if (cur > 1)
            mblock[cblock][cur - 1] = mblock[cblock][cur - 2];
        chkmax(mblock[cblock][cur - 1], a[i]);
        if (cur == BLOCK) {
            rb[cblock++] = i;
            cur = 0;
            lb[cblock] = i + 1;
        }
    }
    if (cur) rb[cblock++] = n - 1;
    for (int cur = 0; cur < cblock; ++cur) {
        for (int i = lb[cur]; i <= rb[cur]; ++i) {
            int cdist = 1, covered = mblock[cur][i - lb[cur]], mx = covered;
            for (int j = i + 1; j <= rb[cur]; ++j) {
                if (j > covered) {
                    ++cdist;
                    covered = mx;
                }
                sumdist[cur][i - lb[cur]] += cdist;
                chkmax(mx, a[j]);
            }
            blockdist[cur][i - lb[cur]] = {cdist, covered};
        }
    }
}

ll get(int num) {
    ll res = 0;
    int cur = numblock[num];
    // counting for this block
    int cdist = 1, covered = a[num], mx = a[num];
    for (int j = num + 1; j <= rb[cur]; ++j) {
        if (j > covered) {
            ++cdist;
            covered = mx;
        }
        res += cdist;
        chkmax(mx, a[j]);
    }
    if (covered < rb[cur]) cout << 1 / 0;
    if (covered == rb[cur]) {
        ++cdist;
        covered = mx;
    }
    // cout << num << ' ' << res << endl;
    for (++cur; cur < cblock; ++cur) {
        // cout << '!' << cur << ' ' << cdist << endl;
        res += 1ll * cdist * (rb[cur] - lb[cur] + 1);
        if (rb[cur] > covered) {
            if (mblock[cur][covered - lb[cur]] >= mx) {
                res += sumdist[cur][covered - lb[cur]];
                cdist += blockdist[cur][covered - lb[cur]].f;
                covered = blockdist[cur][covered - lb[cur]].s;
            }
            else {
                ++cdist;
                res += min(rb[cur], 1ll * mx) - covered;
                covered = mx;
                if (rb[cur] > covered) {
                    res += sumdist[cur][covered - lb[cur]];
                    cdist += blockdist[cur][covered - lb[cur]].f;
                    covered = blockdist[cur][covered - lb[cur]].s;
                }
            }
        }
        chkmax(mx, mblock[cur][rb[cur] - lb[cur]]);
        if (covered < rb[cur]) cout << 1 / 0;
        if (covered == rb[cur]) {
            ++cdist;
            covered = mx;
        }
    }
    // cout << num << ' ' << res << endl << endl;
    return res;
}

int main() {
    IO;
    // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
        cin >> a[i], --a[i];
    a[n - 1] = n - 1;
    init();
    // for (int i = 0; i < n; ++i)
    //     cout << sumdist[numblock[i]][i - lb[numblock[i]]] << ' ';
    ll ans = 0;
    for (int i = 0; i < n; ++i) ans += get(i);
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define f first
#define s second
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}

const int MAXN = 1e5 + 1, BLOCK = 300, CBLOCKS = MAXN / BLOCK + 10;
ll a[MAXN];
ll lb[CBLOCKS], rb[CBLOCKS], numblock[MAXN], mblock[CBLOCKS];
ll sumdist[CBLOCKS][BLOCK];
pair<ll, ll> blockdist[CBLOCKS][BLOCK];
ll n, cblock;

void init() {
    lb[0] = 0;
    cblock = 0;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        ++cur;
        numblock[i] = cblock;
        chkmax(mblock[cblock], a[i]);
        if (cur == BLOCK) {
            rb[cblock++] = i;
            cur = 0;
            lb[cblock] = i + 1;
        }
    }
    if (cur) rb[cblock++] = n - 1;
    for (int cur = 0; cur < cblock; ++cur) {
        int mx1 = 0;
        for (int i = lb[cur]; i <= rb[cur]; ++i) {
            chkmax(mx1, a[i]);
            int cdist = 1, covered = mx1, mx = mx1;
            for (int j = i + 1; j <= rb[cur]; ++j) {
                if (j > covered) {
                    ++cdist;
                    covered = mx;
                }
                sumdist[cur][i - lb[cur]] += cdist;
                chkmax(mx, a[j]);
            }
            blockdist[cur][i - lb[cur]] = {cdist, covered};
        }
    }
}

ll get(int num) {
    ll res = 0;
    int cur = numblock[num];
    // counting for this block
    int cdist = 1, covered = a[num], mx = a[num];
    for (int j = num + 1; j <= rb[cur]; ++j) {
        if (j > covered) {
            ++cdist;
            covered = mx;
        }
        res += cdist;
        chkmax(mx, a[j]);
    }
    if (covered <= rb[cur]) {
        ++cdist;
        covered = mx;
    }
    // cout << num << ' ' << res << endl;
    for (++cur; cur < cblock; ++cur) {
        // cout << '!' << cur << ' ' << cdist << endl;
        res += 1ll * cdist * (rb[cur] - lb[cur] + 1);
        if (rb[cur] > covered) {
            res += sumdist[cur][covered - lb[cur]];
            cdist += blockdist[cur][covered - lb[cur]].f;
            covered = blockdist[cur][covered - lb[cur]].s;
        }
        chkmax(mx, mblock[cur]);
        if (covered <= rb[cur]) {
            ++cdist;
            covered = mx;
        }
    }
    // cout << num << ' ' << res << endl << endl;
    return res;
}

int main() {
    IO;
    // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
        cin >> a[i], --a[i];
    a[n - 1] = n - 1;
    init();
    // for (int i = 0; i < n; ++i)
    //     cout << sumdist[numblock[i]][i - lb[numblock[i]]] << ' ';
    ll ans = 0;
    for (int i = 0; i < n; ++i) ans += get(i);
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define f first
#define s second
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}

const int MAXN = 1e5 + 1, BLOCK = 300, CBLOCKS = MAXN / BLOCK + 10;
ll a[MAXN];
ll lb[CBLOCKS], rb[CBLOCKS], numblock[MAXN], mblock[CBLOCKS];
ll sumdist[CBLOCKS][BLOCK];
pair<ll, ll> blockdist[CBLOCKS][BLOCK];
ll n, cblock;

void init() {
    lb[0] = 0;
    cblock = 0;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        ++cur;
        numblock[i] = cblock;
        chkmax(mblock[cblock], a[i]);
        if (cur == BLOCK) {
            rb[cblock++] = i;
            cur = 0;
            lb[cblock] = i + 1;
        }
    }
    if (cur) rb[cblock++] = n - 1;
    for (int cur = 0; cur < cblock; ++cur) {
        int mx1 = 0;
        for (int i = lb[cur]; i <= rb[cur]; ++i) {
            chkmax(mx1, a[i]);
            int cdist = 1, covered = a[i], mx = mx1;
            for (int j = i + 1; j <= rb[cur]; ++j) {
                if (j > covered) {
                    ++cdist;
                    covered = mx;
                }
                sumdist[cur][i - lb[cur]] += cdist;
                chkmax(mx, a[j]);
            }
            blockdist[cur][i - lb[cur]] = {cdist, covered};
        }
    }
}

ll get(int num) {
    ll res = 0;
    int cur = numblock[num];
    // counting for this block
    int cdist = 1, covered = a[num], mx = a[num];
    for (int j = num + 1; j <= rb[cur]; ++j) {
        if (j > covered) {
            ++cdist;
            covered = mx;
        }
        res += cdist;
        chkmax(mx, a[j]);
    }
    if (covered <= rb[cur]) {
        ++cdist;
        covered = mx;
    }
    // cout << num << ' ' << res << endl;
    for (++cur; cur < cblock; ++cur) {
        // cout << '!' << cur << ' ' << cdist << endl;
        res += 1ll * (cdist - 1) * (rb[cur] - lb[cur] + 1);
        if (rb[cur] > covered) {
            res += sumdist[cur][covered - lb[cur]];
            cdist += blockdist[cur][covered - lb[cur]].f;
            covered = blockdist[cur][covered - lb[cur]].s;
        }
        chkmax(mx, mblock[cur]);
        if (covered <= rb[cur]) {
            ++cdist;
            covered = mx;
        }
    }
    // cout << num << ' ' << res << endl << endl;
    return res;
}

int main() {
    IO;
    // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
        cin >> a[i], --a[i];
    a[n - 1] = n - 1;
    init();
    // for (int i = 0; i < n; ++i)
    //     cout << sumdist[numblock[i]][i - lb[numblock[i]]] << ' ';
    ll ans = 0;
    for (int i = 0; i < n; ++i) ans += get(i);
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define f first
#define s second
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}

const int MAXN = 1e5 + 1, BLOCK = 300, CBLOCKS = MAXN / BLOCK + 10;
ll a[MAXN];
ll lb[CBLOCKS], rb[CBLOCKS], numblock[MAXN], mblock[CBLOCKS];
ll sumdist[CBLOCKS][BLOCK];
pair<ll, ll> blockdist[CBLOCKS][BLOCK];
ll n, cblock;

void init() {
    lb[0] = 0;
    cblock = 0;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        ++cur;
        numblock[i] = cblock;
        chkmax(mblock[cblock], a[i]);
        if (cur == BLOCK) {
            rb[cblock++] = i;
            cur = 0;
            lb[cblock] = i + 1;
        }
    }
    if (cur) rb[cblock++] = n - 1;
    for (int cur = 0; cur < cblock; ++cur) {
        int mx1 = 0;
        for (int i = lb[cur]; i <= rb[cur]; ++i) {
            chkmax(mx1, a[i]);
            int cdist = 1, covered = a[i], mx = mx1;
            for (int j = i + 1; j <= rb[cur]; ++j) {
                if (j > covered) {
                    ++cdist;
                    covered = mx;
                }
                sumdist[cur][i - lb[cur]] += cdist;
                chkmax(mx, a[j]);
            }
            blockdist[cur][i - lb[cur]] = {cdist, covered};
        }
    }
    // for (int i = 0; i < cblock; ++i) {
    //     // cout << "block " << i << ": ";
    //     // cout << lb[i] << ' ' << rb[i] << endl;

    // }
}

ll get(int num) {
    ll res = 0;
    int cur = numblock[num];
    // counting for this block
    int cdist = 1, covered = a[num], mx = a[num];
    for (int j = num + 1; j <= rb[cur]; ++j) {
        if (j > covered) {
            ++cdist;
            covered = mx;
        }
        res += cdist;
        chkmax(mx, a[j]);
    }
    if (covered <= rb[cur]) {
        ++cdist;
        covered = mx;
    }
    // cout << num << ' ' << res << endl;
    for (++cur; cur < cblock; ++cur) {
        // cout << '!' << cur << ' ' << cdist << endl;
        res += 1ll * cdist * (rb[cur] - lb[cur] + 1);
        if (rb[cur] > covered) {
            res += sumdist[cur][covered - lb[cur]];
            cdist += blockdist[cur][covered - lb[cur]].f;
            covered = blockdist[cur][covered - lb[cur]].s;
        }
        chkmax(mx, mblock[cur]);
        if (covered == rb[cur]) {
            ++cdist;
            covered = mx;
        }
    }
    // cout << num << ' ' << res << endl << endl;
    return res;
}

int main() {
    IO;
    // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
        cin >> a[i], --a[i];
    a[n - 1] = n - 1;
    init();
    // for (int i = 0; i < n; ++i)
    //     cout << sumdist[numblock[i]][i - lb[numblock[i]]] << ' ';
    ll ans = 0;
    for (int i = 0; i < n; ++i)
        ans += get(i);
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define f first
#define s second
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}

const int MAXN = 1e5 + 1, BLOCK = 300, CBLOCKS = MAXN / BLOCK + 10;
ll a[MAXN];
ll lb[CBLOCKS], rb[CBLOCKS], numblock[MAXN], mblock[CBLOCKS];
ll sumdist[CBLOCKS][BLOCK];
pair<ll, ll> blockdist[CBLOCKS][BLOCK];
ll n, cblock;

void init() {
    lb[0] = 0;
    cblock = 0;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        ++cur;
        numblock[i] = cblock;
        chkmax(mblock[cblock], a[i]);
        if (cur == BLOCK) {
            rb[cblock++] = i;
            cur = 0;
            lb[cblock] = i + 1;
        }
    }
    if (cur) rb[cblock++] = n - 1;
    for (int cur = 0; cur < cblock; ++cur) {
        int mx1 = 0;
        for (int i = lb[cur]; i <= rb[cur]; ++i) {
            chkmax(mx1, a[i]);
            int cdist = 1, covered = a[i], mx = mx1;
            for (int j = i + 1; j <= rb[cur]; ++j) {
                if (j > covered) {
                    ++cdist;
                    covered = mx;
                }
                sumdist[cur][i - lb[cur]] += cdist;
                chkmax(mx, a[j]);
            }
            blockdist[cur][i - lb[cur]] = {cdist, covered};
        }
    }
}

ll get(int num) {
    ll res = 0;
    int cur = numblock[num];
    // counting for this block
    int cdist = 1, covered = a[num], mx = a[num];
    for (int j = num + 1; j <= rb[cur]; ++j) {
        if (j > covered) {
            ++cdist;
            covered = mx;
        }
        res += cdist;
        chkmax(mx, a[j]);
    }
    if (covered <= rb[cur]) {
        ++cdist;
        covered = mx;
    }
    // cout << num << ' ' << res << endl;
    for (++cur; cur < cblock; ++cur) {
        // cout << '!' << cur << ' ' << cdist << endl;
        if (rb[cur] > covered) {
            res += 1ll * cdist * (covered - lb[cur] + 1);
            res += sumdist[cur][covered - lb[cur]];
            cdist += blockdist[cur][covered - lb[cur]].f;
            covered = blockdist[cur][covered - lb[cur]].s;
        }
        else
            res += 1ll * cdist * (rb[cur] - lb[cur] + 1);
        chkmax(mx, mblock[cur]);
        if (covered <= rb[cur]) {
            ++cdist;
            covered = mx;
        }
    }
    // cout << num << ' ' << res << endl << endl;
    return res;
}

int main() {
    IO;
    // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
        cin >> a[i], --a[i];
    a[n - 1] = n - 1;
    init();
    // for (int i = 0; i < n; ++i)
    //     cout << sumdist[numblock[i]][i - lb[numblock[i]]] << ' ';
    ll ans = 0;
    for (int i = 0; i < n; ++i) ans += get(i);
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define f first
#define s second
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}

const int MAXN = 1e5 + 1, BLOCK = 1000, CBLOCKS = MAXN / BLOCK + 10;
ll a[MAXN];
ll lb[CBLOCKS], rb[CBLOCKS], numblock[MAXN], mblock[CBLOCKS];
ll sumdist[CBLOCKS][BLOCK];
pair<ll, ll> blockdist[CBLOCKS][BLOCK];
ll n, cblock;

void init() {
    lb[0] = 0;
    cblock = 0;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        ++cur;
        numblock[i] = cblock;
        chkmax(mblock[cblock], a[i]);
        if (cur == BLOCK) {
            rb[cblock++] = i;
            cur = 0;
            lb[cblock] = i + 1;
        }
    }
    if (cur) rb[cblock++] = n - 1;
    for (int cur = 0; cur < cblock; ++cur) {
        int mx1 = 0;
        for (int i = lb[cur]; i <= rb[cur]; ++i) {
            chkmax(mx1, a[i]);
            int cdist = 1, covered = a[i], mx = mx1;
            for (int j = i + 1; j <= rb[cur]; ++j) {
                if (j > covered) {
                    ++cdist;
                    covered = mx;
                }
                sumdist[cur][i - lb[cur]] += cdist;
                chkmax(mx, a[j]);
            }
            blockdist[cur][i - lb[cur]] = {cdist, covered};
        }
    }
}

ll get(int num) {
    ll res = 0;
    int cur = numblock[num];
    // counting for this block
    int cdist = 1, covered = a[num], mx = a[num];
    for (int j = num + 1; j <= rb[cur]; ++j) {
        if (j > covered) {
            ++cdist;
            covered = mx;
        }
        res += cdist;
        chkmax(mx, a[j]);
    }
    if (covered <= rb[cur]) {
        ++cdist;
        covered = mx;
    }
    // cout << num << ' ' << res << endl;
    for (++cur; cur < cblock; ++cur) {
        // cout << '!' << cur << ' ' << cdist << endl;
        res += 1ll * cdist * (rb[cur] - lb[cur] + 1);
        if (rb[cur] > covered) {
            res += sumdist[cur][covered - lb[cur]];
            cdist += blockdist[cur][covered - lb[cur]].f;
            covered = blockdist[cur][covered - lb[cur]].s;
        }
        chkmax(mx, mblock[cur]);
        if (covered <= rb[cur]) {
            ++cdist;
            covered = mx;
        }
    }
    // cout << num << ' ' << res << endl << endl;
    return res;
}

int main() {
    IO;
    // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
        cin >> a[i], --a[i];
    a[n - 1] = n - 1;
    init();
    // for (int i = 0; i < n; ++i)
    //     cout << sumdist[numblock[i]][i - lb[numblock[i]]] << ' ';
    ll ans = 0;
    for (int i = 0; i < n; ++i) ans += get(i);
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define f first
#define s second
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}

const int MAXN = 1e5 + 1, BLOCK = 300, CBLOCKS = MAXN / BLOCK + 10;
ll a[MAXN];
ll lb[CBLOCKS], rb[CBLOCKS], numblock[MAXN], mblock[CBLOCKS][BLOCK];
ll sumdist[CBLOCKS][BLOCK];
pair<ll, ll> blockdist[CBLOCKS][BLOCK];
ll n, cblock;

void init() {
    lb[0] = 0;
    cblock = 0;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        ++cur;
        numblock[i] = cblock;
        if (cur > 1)
            mblock[cblock][cur - 1] = mblock[cblock][cur - 2];
        chkmax(mblock[cblock][cur - 1], a[i]);
        if (cur == BLOCK) {
            rb[cblock++] = i;
            cur = 0;
            lb[cblock] = i + 1;
        }
    }
    if (cur) rb[cblock++] = n - 1;
    for (int cur = 0; cur < cblock; ++cur) {
        for (int i = lb[cur]; i <= rb[cur]; ++i) {
            int cdist = 1, covered = mblock[cur][i - lb[cur]], mx = covered;
            for (int j = i + 1; j <= rb[cur]; ++j) {
                if (j > covered) {
                    ++cdist;
                    covered = mx;
                }
                sumdist[cur][i - lb[cur]] += cdist;
                chkmax(mx, a[j]);
            }
            blockdist[cur][i - lb[cur]] = {cdist, covered};
        }
    }
}

ll get(int num) {
    ll res = 0;
    int cur = numblock[num];
    // counting for this block
    ll cdist = 1, covered = a[num], mx = a[num];
    for (int j = num + 1; j <= rb[cur]; ++j) {
        if (j > covered) {
            ++cdist;
            covered = mx;
        }
        res += cdist;
        chkmax(mx, a[j]);
    }
    if (covered < rb[cur]) cout << 1 / 0;
    if (covered == rb[cur]) {
        ++cdist;
        covered = mx;
    }
    // cout << num << ' ' << res << endl;
    for (++cur; cur < cblock; ++cur) {
        // cout << '!' << cur << ' ' << cdist << endl;
        res += 1ll * cdist * (rb[cur] - lb[cur] + 1);
        if (rb[cur] > covered) {
            if (mblock[cur][covered - lb[cur]] >= mx) {
                res += sumdist[cur][covered - lb[cur]];
                cdist += blockdist[cur][covered - lb[cur]].f;
                covered = blockdist[cur][covered - lb[cur]].s;
            }
            else {
                ++cdist;
                res += min(rb[cur], 1ll * mx) - covered;
                covered = mx;
                if (rb[cur] > covered) {
                    res += sumdist[cur][covered - lb[cur]];
                    cdist += blockdist[cur][covered - lb[cur]].f;
                    covered = blockdist[cur][covered - lb[cur]].s;
                }
            }
        }
        chkmax(mx, mblock[cur][rb[cur] - lb[cur]]);
        if (covered < rb[cur]) cout << 1 / 0;
        if (covered == rb[cur]) {
            ++cdist;
            covered = mx;
        }
    }
    // cout << num << ' ' << res << endl << endl;
    return res;
}

int main() {
    IO;
    // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
        cin >> a[i], --a[i];
    a[n - 1] = n - 1;
    init();
    // for (int i = 0; i < n; ++i)
    //     cout << sumdist[numblock[i]][i - lb[numblock[i]]] << ' ';
    ll ans = 0;
    for (int i = 0; i < n; ++i) ans += get(i);
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define f first
#define s second
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}

const int MAXN = 1e5 + 1, BLOCK = 300, CBLOCKS = MAXN / BLOCK + 10;
ll a[MAXN];
ll lb[CBLOCKS], rb[CBLOCKS], numblock[MAXN], mblock[CBLOCKS][BLOCK];
ll sumdist[CBLOCKS][BLOCK];
pair<ll, ll> blockdist[CBLOCKS][BLOCK];
ll n, cblock;

void init() {
    lb[0] = 0;
    cblock = 0;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        ++cur;
        numblock[i] = cblock;
        if (cur > 1)
            mblock[cblock][cur - 1] = mblock[cblock][cur - 2];
        chkmax(mblock[cblock][cur - 1], a[i]);
        if (cur == BLOCK) {
            rb[cblock++] = i;
            cur = 0;
            lb[cblock] = i + 1;
        }
    }
    if (cur) rb[cblock++] = n - 1;
    for (int cur = 0; cur < cblock; ++cur) {
        for (int i = lb[cur]; i <= rb[cur]; ++i) {
            int cdist = 1, covered = mblock[cur][i - lb[cur]], mx = covered;
            for (int j = i + 1; j <= rb[cur]; ++j) {
                if (j > covered) {
                    ++cdist;
                    covered = mx;
                }
                sumdist[cur][i - lb[cur]] += cdist;
                chkmax(mx, a[j]);
            }
            blockdist[cur][i - lb[cur]] = {cdist, covered};
        }
    }
}

ll get(int num) {
    ll res = 0;
    int cur = numblock[num];
    // counting for this block
    int cdist = 1, covered = a[num], mx = a[num];
    for (int j = num + 1; j <= rb[cur]; ++j) {
        if (j > covered) {
            ++cdist;
            covered = mx;
        }
        res += cdist;
        chkmax(mx, a[j]);
    }
    if (covered <= rb[cur]) {
        ++cdist;
        covered = mx;
    }
    // cout << num << ' ' << res << endl;
    for (++cur; cur < cblock; ++cur) {
        // cout << '!' << cur << ' ' << cdist << endl;
        res += 1ll * cdist * (rb[cur] - lb[cur] + 1);
        if (rb[cur] > covered) {
            if (mblock[cur][covered - lb[cur]] >= mx) {
                res += sumdist[cur][covered - lb[cur]];
                cdist += blockdist[cur][covered - lb[cur]].f;
                covered = blockdist[cur][covered - lb[cur]].s;
            }
            else {
                ++cdist;
                res += min(rb[cur], 1ll * mx) - covered;
                covered = mx;
                if (rb[cur] > covered) {
                    res += sumdist[cur][covered - lb[cur]];
                    cdist += blockdist[cur][covered - lb[cur]].f;
                    covered = blockdist[cur][covered - lb[cur]].s;
                }
            }
        }
        chkmax(mx, mblock[cur][rb[cur] - lb[cur]]);
        if (covered <= rb[cur]) {
            ++cdist;
            covered = mx;
        }
    }
    // cout << num << ' ' << res << endl << endl;
    return res;
}

int main() {
    IO;
    // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
        cin >> a[i], --a[i];
    a[n - 1] = n - 1;
    init();
    // for (int i = 0; i < n; ++i)
    //     cout << sumdist[numblock[i]][i - lb[numblock[i]]] << ' ';
    ll ans = 0;
    for (int i = 0; i < n; ++i) ans += get(i);
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define f first
#define s second
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}

const int MAXN = 1e5 + 1, BLOCK = 2, CBLOCKS = MAXN / BLOCK + 1;
int a[MAXN];
int lb[CBLOCKS], rb[CBLOCKS], numblock[MAXN], mblock[CBLOCKS];
ll sumdist[CBLOCKS][BLOCK];
pair<int, int> blockdist[CBLOCKS][BLOCK];
int n, cblock;

void init() {
    lb[0] = 0;
    cblock = 0;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        ++cur;
        numblock[i] = cblock;
        chkmax(mblock[cblock], a[i]);
        if (cur == BLOCK) {
            rb[cblock++] = i;
            cur = 0;
            lb[cblock] = i + 1;
        }
    }
    if (cur) rb[cblock++] = n - 1;
    for (int cur = 0; cur < cblock; ++cur) {
        int mx1 = 0;
        for (int i = lb[cur]; i <= rb[cur]; ++i) {
            chkmax(mx1, a[i]);
            int cdist = 1, covered = a[i], mx = mx1;
            for (int j = i + 1; j <= rb[cur]; ++j) {
                if (j > covered) {
                    ++cdist;
                    covered = mx;
                }
                sumdist[cur][i - lb[cur]] += cdist;
                chkmax(mx, a[j]);
            }
            blockdist[cur][i - lb[cur]] = {cdist, covered};
        }
    }
    // for (int i = 0; i < cblock; ++i) {
    //     // cout << "block " << i << ": ";
    //     // cout << lb[i] << ' ' << rb[i] << endl;

    // }
}

ll get(int num) {
    ll res = 0;
    int cur = numblock[num];
    // counting for this block
    int cdist = 1, covered = a[num], mx = a[num];
    for (int j = num + 1; j <= rb[cur]; ++j) {
        if (j > covered) {
            ++cdist;
            covered = mx;
        }
        res += cdist;
        chkmax(mx, a[j]);
    }
    if (covered <= rb[cur]) {
        ++cdist;
        covered = mx;
    }
    // cout << num << ' ' << res << endl;
    for (++cur; cur < cblock; ++cur) {
        // cout << '!' << cur << ' ' << cdist << endl;
        res += 1ll * cdist * (rb[cur] - lb[cur] + 1);
        if (rb[cur] > covered) {
            res += sumdist[cur][covered - lb[cur]];
            cdist += blockdist[cur][covered - lb[cur]].f;
            covered = blockdist[cur][covered - lb[cur]].s;
        }
        chkmax(mx, mblock[cur]);
        if (covered == rb[cur]) {
            ++cdist;
            covered = mx;
        }
    }
    // cout << num << ' ' << res << endl << endl;
    return res;
}

int main() {
    IO;
    // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
        cin >> a[i], --a[i];
    a[n - 1] = n - 1;
    init();
    // for (int i = 0; i < n; ++i)
    //     cout << sumdist[numblock[i]][i - lb[numblock[i]]] << ' ';
    ll ans = 0;
    for (int i = 0; i < n; ++i)
        ans += get(i);
    cout << ans;
    return 0;
}


#include <bits/stdc++.h>

#define IO ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL)
#define f first
#define s second
#define pb push_back
#define all(a) a.begin(), a.end()
using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int ui;
typedef unsigned long long ull;

template<typename T1, typename T2> inline void chkmax(T1 & x, T2 y) {if (x < y) x = y;}
template<typename T1, typename T2> inline void chkmin(T1 & x, T2 y) {if (x > y) x = y;}

const int MAXN = 1e5 + 1, BLOCK = 300, CBLOCKS = MAXN / BLOCK + 10;
ll a[MAXN];
ll lb[CBLOCKS], rb[CBLOCKS], numblock[MAXN], mblock[CBLOCKS][BLOCK];
ll sumdist[CBLOCKS][BLOCK];
pair<ll, ll> blockdist[CBLOCKS][BLOCK];
ll n, cblock;

void init() {
    lb[0] = 0;
    cblock = 0;
    int cur = 0;
    for (int i = 0; i < n; ++i) {
        ++cur;
        numblock[i] = cblock;
        if (cur > 1)
            mblock[cblock][cur - 1] = mblock[cblock][cur - 2];
        else
            mblock[cblock][cur - 1] = 0;
        chkmax(mblock[cblock][cur - 1], a[i]);
        if (cur == BLOCK) {
            rb[cblock++] = i;
            cur = 0;
            lb[cblock] = i + 1;
        }
    }
    if (cur) rb[cblock++] = n - 1;
    for (int cur = 0; cur < cblock; ++cur) {
        // cout << lb[cur] << ' ' << rb[cur] << endl;
        // for (int i = 0; i < BLOCK; ++i)
        //     cout << mblock[cur][i] << ' ';
        // cout << endl;
        for (int i = lb[cur]; i <= rb[cur]; ++i) {
            sumdist[cur][i - lb[cur]] = 0;
            int cdist = 1, covered = mblock[cur][i - lb[cur]], mx = covered;
            for (int j = i + 1; j <= rb[cur]; ++j) {
                if (j > covered) {
                    ++cdist;
                    covered = mx;
                }
                sumdist[cur][i - lb[cur]] += cdist;
                chkmax(mx, a[j]);
            }
            // cout << sumdist[cur][i - lb[cur]] << "--!";
            blockdist[cur][i - lb[cur]] = {cdist, covered};
            // cout << blockdist[cur][i - lb[cur]].f << ' ' << blockdist[cur][i - lb[cur]].s << "|";
        }
        // cout << endl;
    }
}

ll get(int num) {
    ll res = 0;
    int cur = numblock[num];
    // counting for this block
    ll cdist = 1, covered = a[num], mx = a[num];
    for (int j = num + 1; j <= rb[cur]; ++j) {
        if (j > covered) {
            ++cdist;
            covered = mx;
        }
        res += cdist;
        chkmax(mx, a[j]);
    }
    // cout << "! " << res << endl;
    if (covered < rb[cur]) cout << 1 / 0;
    if (covered <= rb[cur]) {
        ++cdist;
        covered = mx;
    }
    // cout << num << ' ' << res << endl;
    for (++cur; cur < cblock; ++cur) {
        // cout << '!' << cur << ' ' << cdist << endl;
        // cout << cdist << ' ' << covered << endl;
        res += 1ll * cdist * (rb[cur] - lb[cur] + 1);
        // cout << "cres = " << res << endl;
        if (rb[cur] > covered) {
            if (mblock[cur][covered - lb[cur]] >= mx) {
                res += sumdist[cur][covered - lb[cur]];
                cdist += blockdist[cur][covered - lb[cur]].f;
                covered = blockdist[cur][covered - lb[cur]].s;
            }
            else {
                // cout << "LUL\n";
                ++cdist;
                res += rb[cur] - covered;
                // cout << min(rb[cur], mx) << ' ' << covered << ' ' << min(rb[cur], mx) - covered << endl;
                covered = mx;
                if (rb[cur] > covered) {
                    res += sumdist[cur][covered - lb[cur]];
                    cdist += blockdist[cur][covered - lb[cur]].f;
                    covered = blockdist[cur][covered - lb[cur]].s;
                }
            }
        }
        chkmax(mx, mblock[cur][rb[cur] - lb[cur]]);
        if (covered < rb[cur]) cout << 1 / 0;
        if (covered == rb[cur]) {
            ++cdist;
            covered = mx;
        }
    }
    // cout << "---" << num << ' ' << res << endl << endl;
    return res;
}

ll solve_stupid() {
    ll res = 0;
    for (int i = 0; i < n; ++i) {
        int kek = 0;
        int mx = a[i], covered = a[i], dist = 1;
        for (int j = i + 1; j < n; ++j) {
            if (j > covered) {
                ++dist;
                covered = mx;
            }
            // res += dist;
            kek += dist;
            chkmax(mx, a[j]);
        }
        res += kek;
        // cout << i << ' ' << kek << endl;
    }
    return res;
}

ll solve_smart() {
    init();
    ll ans = 0;
    // get(2);
    for (int i = 0; i < n; ++i) ans += get(i);
    return ans;
}

int main() {
    IO;
    // freopen("input.txt", "r", stdin);
    cin >> n;
    for (int i = 0; i < n - 1; ++i)
        cin >> a[i], --a[i];
    a[n - 1] = n - 1;
    cout << solve_smart();
    // cout << solve_stupid();
    // int cnt = 0;
    // while (true) {
    //     ++cnt;
    //     if (cnt % 1000000 == 0) {
    //         cout << cnt << endl;
    //     }
    //     n = rand() % 9;
    //     for (int i = 0; i < n - 1; ++i) {
    //         a[i] = min(n - 1, i + 1 + rand() % n);
    //     }
    //     a[n - 1] = n - 1;
    //     if (solve_stupid() != solve_smart()) {
    //         cout << "KEK" << endl;
    //         cout << n << '\n';
    //         for (int i = 0; i < n - 1; ++i)
    //             cout << a[i] + 1 << ' ';
    //         cout << endl;
    //         cout << solve_stupid() << ' ' << solve_smart() << endl;
    //         return 0;
    //     }
    // }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

struct segtree{
    static const int N=1<<17;
    vector<pair<int,int>>dat;

    segtree():dat(N*2){
        for(int i=0;i<N;i++)dat[i+N-1]={0,i};
        for(int i=N-2;i>=0;i--)dat[i]=max(dat[i*2+1],dat[i*2+2]);
    }
    void update(int k,int x){
        k+=N-1;
        dat[k].first=x;
        while(k){
            k=(k-1)/2;
            dat[k]=max(dat[k*2+1],dat[k*2+2]);
        }
    }
    pair<int,int>get(int a,int b,int k=0,int l=0,int r=N){
        if(r<=a||b<=l)return {-1,-1};
        if(a<=l&&r<=b)return dat[k];
        return max(get(a,b,k*2+1,l,(l+r)/2),get(a,b,k*2+2,(l+r)/2,r));
    }
};

int main(){
    int n;
    scanf("%d",&n);
    vector<int>r(n);
    for(int i=0;i<n-1;i++)scanf("%d",&r[i]);

    r[n-1]=n;
    vector<ll>dp(n);

    segtree seg;
    seg.update(n-1,n);
    for(int i=n-2;i>=0;i--){
        int j=seg.get(i,r[i]).second;
        dp[i]=dp[j]+(n-i-1);
        seg.update(i,r[i]);
    }

    printf("%lld\n",accumulate(dp.begin(),dp.end(),0ll));

    return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<sstream>
#include<bitset>
#include<numeric>
using namespace std;

typedef long long ll;

template<int N,typename T,T INF>
struct segment_tree_rmq {
	vector<pair<T, int>>mini, maxi;
	segment_tree_rmq() :mini(1 << (N + 1)), maxi(1 << (N + 1)) {
		for (int i = 0; i < 1 << N; i++) {
			mini[i + (1 << N) - 1] = { INF,i };
			maxi[i + (1 << N) - 1] = { -INF,i };
		}
		for (int i = (1 << N) - 2; i >= 0; i--) {
			mini[i] = min(mini[i * 2 + 1], mini[i * 2 + 2]);
			maxi[i] = max(maxi[i * 2 + 1], maxi[i * 2 + 2]);
		}
	}
	void update(int k, T x) {
		k += (1 << N) - 1;
		mini[k].first = maxi[k].first = x;
		while (k) {
			k = (k - 1) / 2;
			mini[k] = min(mini[k * 2 + 1], mini[k * 2 + 2]);
			maxi[k] = max(maxi[k * 2 + 1], maxi[k * 2 + 2]);
		}
	}

	pair<T, int>get_min(int a, int b, int k = 0, int l = 0, int r = 1 << N) {
		if (r <= a || b <= l)return{ INF,-1 };
		if (a <= l&&r <= b)return mini[k];
		return min(get_min(a, b, k * 2 + 1, l, (l + r) / 2), get_min(a, b, k * 2 + 2, (l + r) / 2, r));
	}

	pair<T, int>get_max(int a, int b, int k = 0, int l = 0, int r = 1 << N) {
		if (r <= a || b <= l)return{ -INF,-1 };
		if (a <= l&&r <= b)return maxi[k];
		return max(get_max(a, b, k * 2 + 1, l, (l + r) / 2), get_max(a, b, k * 2 + 2, (l + r) / 2, r));
	}

	T get_min_val(int a, int b) { return get_min(a, b).first; }
	T get_max_val(int a, int b) { return get_max(a, b).first; }
	int get_min_idx(int a, int b) { return get_min(a, b).second; }
	int get_max_idx(int a, int b) { return get_max(a, b).second; }
};

int main() {
	int n;
	cin >> n;
	vector<int>r(n);
	for (int i = 0; i < n - 1; i++)cin >> r[i];
	r[n - 1] = n;

	segment_tree_rmq<17, int, 1001001001>seg;
	for (int i = 0; i < n; i++)seg.update(i, r[i]);

	vector<ll>dp(n);
	for (int i = n - 2; i >= 0; i--) {
		int j = seg.get_max_idx(i + 1, r[i]);
		dp[i] = dp[j] + (n - 1 - i) - (r[i] - j - 1);
	}

	cout << accumulate(dp.begin(), dp.end(), 0ll) << endl;
	return 0;
}


#include<cstdio>
#include<algorithm>
#include<vector>
#include<iostream>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
#include<map>
#include<set>
#include<sstream>
#include<bitset>
#include<numeric>
using namespace std;

typedef long long ll;

template<int N>
struct segment_tree_max {
	vector<pair<int, int>>dat;
	segment_tree_max():dat(1<<(N+1)) {
		for (int i = 0; i < 1 << N; i++)dat[(1 << N) - 1 + i].second = i;
		for (int i = (1 << N) - 2; i >= 0; i--)dat[i] = max(dat[i * 2 + 1], dat[i * 2 + 2]);
	}
	void update(int k, int x) {
		k += (1 << N) - 1;
		dat[k].first = x;
		while (k) {
			k = (k - 1) / 2;
			dat[k] = max(dat[k * 2 + 1], dat[k * 2 + 2]);
		}
	}
	pair<int,int>get(int a, int b, int k = 0, int l = 0, int r = 1 << N) {
		if (r <= a || b <= l)return{ -1,-1 };
		if (a <= l&&r <= b)return dat[k];
		return max(get(a, b, k * 2 + 1, l, (l + r) / 2), get(a, b, k * 2 + 2, (l + r) / 2, r));
	}
	int get_val(int a, int b) { return get(a, b).first; }
	int get_idx(int a, int b) { return get(a, b).second; }
};

int main() {
	int n;
	cin >> n;
	vector<int>r(n);
	for (int i = 0; i < n - 1; i++)cin >> r[i];
	r[n - 1] = n;

	segment_tree_max<17>seg;
	for (int i = 0; i < n; i++)seg.update(i, r[i]);

	vector<ll>dp(n);

	for (int i = n - 2; i >= 0; i--) {
		int j = seg.get_idx(i + 1, r[i]);
		dp[i] = dp[j] + (n - i - 1) - (r[i] - j - 1);
	}

	cout << accumulate(dp.begin(), dp.end(), 0ll) << endl;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>inline void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>inline void chmax(T &t,U f){if(t<f)t=f;}

struct segtree{
    static const int SEG=1<<17;
    vint sum,put;
    segtree():sum(SEG*2),put(SEG*2){}

    inline void push(int k,int l,int r){
        sum[k]+=put[k]*(r-l);
        if(k<SEG-1){
            put[k*2+1]+=put[k];
            put[k*2+2]+=put[k];
        }
        put[k]=0;
    }
    void add(int a,int b,int x,int k=0,int l=0,int r=SEG){
        push(k,l,r);
        if(r<=a||b<=l)return;
        if(a<=l&&r<=b){
            put[k]+=x;
            push(k,l,r);
            return;
        }
        add(a,b,x,k*2+1,l,(l+r)/2);
        add(a,b,x,k*2+2,(l+r)/2,r);
        sum[k]=sum[k*2+1]+sum[k*2+2];
    }
    int get(int a,int b,int k=0,int l=0,int r=SEG){
        push(k,l,r);
        if(r<=a||b<=l)return 0;
        if(a<=l&&r<=b)return sum[k];
        return get(a,b,k*2+1,l,(l+r)/2)+get(a,b,k*2+2,(l+r)/2,r);
    }
};

int N;
int A[100000];

signed main(){
    scanf("%lld",&N);
    rep(i,N-1)scanf("%lld",&A[i]);
    vpint vec;
    segtree seg;

    int ans=0;
    for(int i=N-2;i>=0;i--){
        while(vec.size()){
            pint p(-1,-1);
            if(vec.back().se<=A[i]){
                p=vec.back();
                vec.pop_back();
            }
            if(p.fi==-1)break;
            seg.add(p.fi,N,-1);
            if(vec.size()){
                seg.add(vec.back().fi,p.se,1);
            }
        }
        if(vec.size()){
            seg.add(vec.back().fi,A[i],-1);
        }
        seg.add(i,N,1);
        vec.push_back(pint(i,A[i]));
        ans+=seg.get(i+1,N);
    }
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

struct segtree{
    static const int N=1<<17;
    vector<pair<int,int>>dat;

    segtree():dat(N*2){
        for(int i=0;i<N;i++)dat[i+N-1]={0,i};
        for(int i=N-2;i>=0;i--)dat[i]=max(dat[i*2+1],dat[i*2+2]);
    }
    void update(int k,int x){
        k+=N-1;
        dat[k].first=x;
        while(k){
            k=(k-1)/2;
            dat[k]=max(dat[k*2+1],dat[k*2+2]);
        }
    }
    pair<int,int>get(int a,int b,int k=0,int l=0,int r=N){
        if(r<=a||b<=l)return {-1,-1};
        if(a<=l&&r<=b)return dat[k];
        return max(get(a,b,k*2+1,l,(l+r)/2),get(a,b,k*2+2,(l+r)/2,r));
    }
};

int main(){
    int n;
    scanf("%d",&n);
    vector<int>r(n);
    for(int i=0;i<n-1;i++)scanf("%d",&r[i]);

    r[n-1]=n;
    vector<ll>dp(n);

    segtree seg;
    for(int i=0;i<n;i++)seg.update(i,r[i]);

    for(int i=n-2;i>=0;i--){
        int j=seg.get(i+1,r[i]).second;
        dp[i]=dp[j]+n-i-1-(r[i]-j-1);
    }

    printf("%lld\n",accumulate(dp.begin(),dp.end(),0ll));

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;

struct segtree{
    static const int N=1<<17;
    vector<pair<int,int>>dat;

    segtree():dat(N*2){
        for(int i=0;i<N;i++)dat[i+N-1]={0,i};
        for(int i=N-2;i>=0;i--)dat[i]=max(dat[i*2+1],dat[i*2+2]);
    }
    void update(int k,int x){
        k+=N-1;
        dat[k].first=x;
        while(k){
            k=(k-1)/2;
            dat[k]=max(dat[k*2+1],dat[k*2+2]);
        }
    }
    pair<int,int>get(int a,int b,int k=0,int l=0,int r=N){
        if(r<=a||b<=l)return {-1,-1};
        if(a<=l&&r<=b)return dat[k];
        return max(get(a,b,k*2+1,l,(l+r)/2),get(a,b,k*2+2,(l+r)/2,r));
    }
};

int main(){
    int n;
    scanf("%d",&n);
    vector<int>r(n);
    for(int i=0;i<n-1;i++)scanf("%d",&r[i]);

    r[n-1]=n;
    vector<ll>dp(n);

    segtree seg;
    seg.update(n-1,n);
    for(int i=n-2;i>=0;i--){
        int j=seg.get(i,r[i]).second;
        dp[i]=dp[j]+(n-i-1);
    }

    printf("%lld\n",accumulate(dp.begin(),dp.end(),0ll));

    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define int long long
typedef pair<int,int>pint;
typedef vector<int>vint;
typedef vector<pint>vpint;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(v) (v).begin(),(v).end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define reps(i,f,n) for(int i=(f);i<(n);i++)
#define each(it,v) for(__typeof((v).begin()) it=(v).begin();it!=(v).end();it++)
template<class T,class U>inline void chmin(T &t,U f){if(t>f)t=f;}
template<class T,class U>inline void chmax(T &t,U f){if(t<f)t=f;}

struct segtree{
    static const int SEG=1<<17;
    vint sum,put;
    segtree():sum(SEG*2),put(SEG*2){}

    inline void push(int k,int l,int r){
        sum[k]+=put[k]*(r-l);
        if(k<SEG-1){
            put[k*2+1]+=put[k];
            put[k*2+2]+=put[k];
        }
        put[k]=0;
    }
    void add(int a,int b,int x,int k=0,int l=0,int r=SEG){
        push(k,l,r);
        if(r<=a||b<=l)return;
        if(a<=l&&r<=b){
            put[k]+=x;
            push(k,l,r);
            return;
        }
        add(a,b,x,k*2+1,l,(l+r)/2);
        add(a,b,x,k*2+2,(l+r)/2,r);
        sum[k]=sum[k*2+1]+sum[k*2+2];
    }
    int get(int a,int b,int k=0,int l=0,int r=SEG){
        push(k,l,r);
        if(r<=a||b<=l)return 0;
        if(a<=l&&r<=b)return sum[k];
        return get(a,b,k*2+1,l,(l+r)/2)+get(a,b,k*2+2,(l+r)/2,r);
    }
};

int N;
int A[100000];

signed main(){
    scanf("%lld",&N);
    rep(i,N-1)scanf("%lld",&A[i]);
    vpint vec;
    segtree seg;

    int ans=0;
    for(int i=N-2;i>=0;i--){
        while(vec.size()){
            pint p(-1,-1);
            if(vec.back().se<=A[i]){
                p=vec.back();
                vec.pop_back();
            }
            else if(vec.size()>=2&&vec[vec.size()-2].fi<A[i]){
                p=vec.back();
                vec.pop_back();
            }
            if(p.fi==-1)break;
            seg.add(p.fi,N,-1);
            if(vec.size()){
                seg.add(vec.back().fi,p.se,1);
            }
        }
        if(vec.size()){
            seg.add(vec.back().fi,A[i],-1);
        }
        seg.add(i,N,1);
        vec.push_back(pint(i,A[i]));
        ans+=seg.get(i+1,N);
    }
    printf("%lld\n",ans);
    return 0;
}


/*****************************************************/
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define   sigma_size    26
#define   lson          l,m,v<<1
#define   rson          m+1,r,v<<1|1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   mk            make_pair

const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 1e18;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-9;
const LL     mod   = 1e9+7;
const int    maxmat= 10;

/*****************************************************/

const int MAX = 1e5 + 5;
int a[MAX],dp[MAX],seg[MAX<<2];
int Max(int x,int y){
	return (a[x] > a[y] ? x : y);
}
void PushUp(int v){
	seg[v] = Max (seg[v<<1],seg[v<<1|1]);
}
void build(int l,int r,int v){
	if (l == r) seg[v] = l;
	else{
		int m = (l + r)>>1;
		build(lson);
		build(rson);
		PushUp(v);
	}
}
int query(int L,int R,int l,int r,int v){
	if (L <= l && r <= R) return seg[v];
	int m = (l + r)>>1;
	if (R <= m) return query(L,R,lson);
	else if (L > m) return query(L,R,rson);
	else return Max(query(L,R,lson),query(L,R,rson));
}
int main(int argc, char const *argv[])
{
	int N;
	LL ans = 0;
	cin>>N;
	for (int i = 1;i < N;i++)
		scanf("%d",a + i);
	a[N] = N;
	build(1,N,1);
	for (int i = N-1;i>0;i--){
		// cout<<"q"<<i+1<<" "<<a[i]<<endl;
		int temp = query(i+1,a[i],1,N,1);
		dp[i] = dp[temp] + (LL)(N-i-(a[i]-temp));
		// cout<<temp<<" "<<dp[i]<<endl;
		ans += dp[i];
	}
	cout<<ans<<endl;
	return 0;
}


/*****************************************************/
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define   sigma_size    26
#define   lson          l,m,v<<1
#define   rson          m+1,r,v<<1|1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   mk            make_pair

const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 1e18;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-9;
const LL     mod   = 1e9+7;
const int    maxmat= 10;

/*****************************************************/

const int MAX = 1e5 + 5;
int a[MAX],dp[MAX],seg[MAX<<2];
int Max(int x,int y){
	return (a[x] > a[y] ? x : y);
}
void PushUp(int v){
	seg[v] = Max (seg[v<<1],seg[v<<1|1]);
}
void build(int l,int r,int v){
	if (l == r) seg[v] = l;
	else{
		int m = (l + r)>>1;
		build(lson);
		build(rson);
		PushUp(v);
	}
}
int query(int L,int R,int l,int r,int v){
	if (L <= l && r <= R) return seg[v];
	int m = (l + r)>>1;
	if (R <= m) return query(L,R,lson);
	else if (L > m) return query(L,R,rson);
	else{
		int fl = query(L,R,lson),fr = query(L,R,rson);
		if (a[fl] > a[fr]) return fl;
		return fr;
	}
	// else return Max (query(L,R,lson),query(L,R,rson));
}
int main(int argc, char const *argv[])
{
	int N;
	LL ans = 0;
	cin>>N;
	for (int i = 1;i < N;i++)  scanf("%d",a + i);
	a[N] = N-1;
	build(1,N,1);
	for (int i = N-1;i>0;i--){
		int temp = query(i+1,a[i],1,N,1);
		dp[i] = dp[temp] + (LL)(N-i-(a[i]-temp));
		cout<<temp<<" "<<dp[i]<<endl;
		ans += dp[i];
	}
	cout<<ans<<endl;
	return 0;
}


/*****************************************************/
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define   sigma_size    26
#define   lson          l,m,v<<1
#define   rson          m+1,r,v<<1|1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   mk            make_pair

const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 1e18;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-9;
const LL     mod   = 1e9+7;
const int    maxmat= 10;

/*****************************************************/

const int MAX = 1e5 + 5;
int a[MAX],dp[MAX],seg[MAX<<2];
int Max(int x,int y){
	return (a[x] >= a[y] ? x : y);
}
void PushUp(int v){
	seg[v] = Max (seg[v<<1],seg[v<<1|1]);
}
void build(int l,int r,int v){
	if (l == r) seg[v] = l;
	else{
		int m = (l + r)>>1;
		build(lson);
		build(rson);
		PushUp(v);
	}
}
int query(int L,int R,int l,int r,int v){
	if (L <= l && r <= R) return seg[v];
	int m = (l + r)>>1;
	if (R <= m) return query(L,R,lson);
	else if (L > m) return query(L,R,rson);
	else return Max(query(L,R,lson),query(L,R,rson));
}
int main(int argc, char const *argv[])
{
	int N;
	LL ans = 0;
	cin>>N;
	for (int i = 1;i < N;i++)
		scanf("%d",a + i);
	a[N] = N;
	build(1,N,1);
	for (int i = N-1;i>0;i--){
		// cout<<"q"<<i+1<<" "<<a[i]<<endl;
		int temp = query(i+1,a[i],1,N,1);
		dp[i] = dp[temp] + (LL)(N-i-(a[i]-temp));
		// cout<<temp<<" "<<dp[i]<<endl;
		ans += dp[i];
	}
	cout<<ans<<endl;
	return 0;
}


/*****************************************************/
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define   sigma_size    26
#define   lson          l,m,v<<1
#define   rson          m+1,r,v<<1|1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   mk            make_pair

const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 1e18;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-9;
const LL     mod   = 1e9+7;
const int    maxmat= 10;

/*****************************************************/

const int MAX = 1e5 + 5;
int a[MAX],dp[MAX],seg[MAX<<2];
int Max(int x,int y){
	return (a[x] > a[y] ? x : y);
}
void PushUp(int v){
	seg[v] = Max (seg[v<<1],seg[v<<1|1]);
}
void build(int l,int r,int v){
	if (l == r) seg[v] = l;
	else{
		int m = (l + r)>>1;
		build(lson);
		build(rson);
		PushUp(v);
	}
}
int query(int L,int R,int l,int r,int v){
	if (L <= l && r <= R) return seg[v];
	int m = (l + r)>>1;
	if (R <= m) return query(L,R,lson);
	else if (L > m) return query(L,R,rson);
	else{
		int fl = query(L,R,lson),fr = query(L,R,rson);
		if (a[fl] > a[fr]) return fl;
		return fr;
	}
	// else return Max (query(L,R,lson),query(L,R,rson));
}
int main(int argc, char const *argv[])
{
	int N;
	LL ans = 0;
	cin>>N;
	for (int i = 1;i < N;i++)  scanf("%d",a + i);
	a[N] = N-1;
	build(1,N,1);
	for (int i = N-1;i>0;i--){
		int temp = query(i+1,a[i],1,N,1);
		dp[i] = dp[temp] + (LL)(N-i-(a[i]-temp));
		//cout<<temp<<" "<<dp[i]<<endl;
		ans += dp[i];
	}
	cout<<ans<<endl;
	return 0;
}


/*****************************************************/
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define   sigma_size    26
#define   lson          l,m,v<<1
#define   rson          m+1,r,v<<1|1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   mk            make_pair

const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 1e18;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-9;
const LL     mod   = 1e9+7;
const int    maxmat= 10;

/*****************************************************/

const int MAX = 1e5 + 5;
int a[MAX],seg[MAX<<2];
LL dp[MAX];
int Max(int x,int y){
	return (a[x] > a[y] ? x : y);
}
void PushUp(int v){
	seg[v] = Max (seg[v<<1],seg[v<<1|1]);
}
void build(int l,int r,int v){
	if (l == r) seg[v] = l;
	else{
		int m = (l + r)>>1;
		build(lson);
		build(rson);
		PushUp(v);
	}
}
int query(int L,int R,int l,int r,int v){
	if (L <= l && r <= R) return seg[v];
	int m = (l + r)>>1;
	if (R <= m) return query(L,R,lson);
	else if (L > m) return query(L,R,rson);
	else{
		int fl = query(L,R,lson),fr = query(L,R,rson);
		if (a[fl] > a[fr]) return fl;
		return fr;
	}
	// else return Max (query(L,R,lson),query(L,R,rson));
}
int main(int argc, char const *argv[])
{
	int N;
	LL ans = 0;
	cin>>N;
	for (int i = 1;i < N;i++)  scanf("%d",a + i);
	a[N] = N-1;
	build(1,N,1);
	for (int i = N-1;i>0;i--){
		int temp = query(i+1,a[i],1,N,1);
		dp[i] = dp[temp] + (LL)(N-i-(a[i]-temp));
		// cout<<temp<<" "<<dp[i]<<endl;
		ans += dp[i];
	}
	cout<<ans<<endl;
	return 0;
}


/*****************************************************/
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define   sigma_size    26
#define   lson          l,m,v<<1
#define   rson          m+1,r,v<<1|1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   mk            make_pair

const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 1e18;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-9;
const LL     mod   = 1e9+7;
const int    maxmat= 10;

/*****************************************************/

const int MAX = 1e5 + 5;
int a[MAX],dp[MAX],seg[MAX<<2];
int Max(int x,int y){
	return (a[x] > a[y] ? x : y);
}
void PushUp(int v){
	seg[v] = Max (seg[v<<1],seg[v<<1|1]);
}
void build(int l,int r,int v){
	if (l == r) seg[v] = l;
	else{
		int m = (l + r)>>1;
		build(lson);
		build(rson);
		PushUp(v);
	}
}
int query(int L,int R,int l,int r,int v){
	if (L <= l && r <= R) return seg[v];
	int m = (l + r)>>1;
	if (R <= m) return query(L,R,lson);
	else if (L >= m) return query(L,R,rson);
	else return Max(query(L,R,lson),query(L,R,rson));
}
int main(int argc, char const *argv[])
{
	int N;
	LL ans = 0;
	cin>>N;
	for (int i = 1;i < N;i++)
		scanf("%d",a + i);
	a[N] = N;
	build(1,N,1);
	for (int i = N-1;i>0;i--){
		// cout<<"q"<<i+1<<" "<<a[i]<<endl;
		int temp = query(i+1,a[i],1,N,1);
		// cout<<temp<<endl;
		dp[i] = dp[temp] + (LL)(N-i-(a[i]-temp));
		ans += dp[i];
	}
	cout<<ans<<endl;
	return 0;
}


/*****************************************************/
#include <map>
#include <set>
#include <ctime>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <cstdio>
#include <cctype>
#include <cstring>
#include <sstream>
#include <cstdlib>
#include <iostream>
#include <algorithm>
using namespace std;
#define   sigma_size    26
#define   lson          l,m,v<<1
#define   rson          m+1,r,v<<1|1
#define   LL            long long
#define   ull           unsigned long long
#define   mem(x,v)      memset(x,v,sizeof(x))
#define   lowbit(x)     (x&-x)
#define   mk            make_pair

const int    INF   = 0x3f3f3f3f;
const LL     INFF  = 1e18;
const double pi    = acos(-1.0);
const double inf   = 1e18;
const double eps   = 1e-9;
const LL     mod   = 1e9+7;
const int    maxmat= 10;

/*****************************************************/

const int MAX = 1e5 + 5;
int a[MAX],dp[MAX],seg[MAX<<2];
int Max(int x,int y){
	return (a[x] > a[y] ? x : y);
}
void PushUp(int v){
	seg[v] = Max (seg[v<<1],seg[v<<1|1]);
}
void build(int l,int r,int v){
	if (l == r) seg[v] = l;
	else{
		int m = (l + r)>>1;
		build(lson);
		build(rson);
		PushUp(v);
	}
}
int query(int L,int R,int l,int r,int v){
	if (L <= l && r <= R) return seg[v];
	int m = (l + r)>>1;
	if (R <= m) return query(L,R,lson);
	else if (L > m) return query(L,R,rson);
	else return Max(query(L,R,lson),query(L,R,rson));
}
int main(int argc, char const *argv[])
{
	int N;
	LL ans = 0;
	cin>>N;
	for (int i = 1;i < N;i++)
		scanf("%d",a + i);
	a[N] = N-1;
	build(1,N,1);
	for (int i = N-1;i>0;i--){
		// cout<<"q"<<i+1<<" "<<a[i]<<endl;
		int temp = query(i+1,a[i],1,N,1);
		dp[i] = dp[temp] + (LL)(N-i-(a[i]-temp));
		// cout<<temp<<" "<<dp[i]<<endl;
		ans += dp[i];
	}
	cout<<ans<<endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
using namespace std;

#define M 100005
#define LL long long

int n, a[M], tail, s[M];
LL ans, f[M];

int main (){
    scanf ("%d", &n);
    for (int i = 1; i < n; i ++)
        scanf ("%d", &a[i]);
    f[n - 1] = ans = 1;
    tail = 1;
    s[tail] = n - 1;
    for (int i = n - 2; i >= 1; i --){
        int k = *(upper_bound (s + 1, s + 1 + tail, a[i]) - 1);
        f[i] = f[k] + (k - i) + (n - a[i]);
        while (tail && a[i] >= a[s[tail]])
            tail --;
        s[++ tail] = i;
        ans += f[i];
    }
    printf ("%lld\n", ans);
    return 0;
}


#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
using namespace std;
int n,a[100005],tail,Q[1000005];
long long ans = 1,dp[100005];
int main(){
	scanf ("%d",&n);
	for (int i = 1;i < n;i ++)
		scanf ("%d",&a[i]);
	dp[n - 1] = 1;
	tail = 1;
	Q[1] = n - 1;
	for (int i = n - 2;i >= 1;i --){
		int k = *(lower_bound(Q + 1 + tail,Q + 1,a[i]) - 1);
		dp[i] = dp[k] + (k - i) + (n - a[i]);
		ans += dp[i];
		while (1 <= tail && a[i] >= a[Q[tail]])
			tail --;
		Q[++ tail] = i;
	}
	printf("%lld\n",ans);
}


#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
struct tree{
        int l,r,val,idx;
}t[1200005];
int n,m,a[300005];
long long dp[300005],ans=0;
int read() {
        register int x=0,w=1;char ch=getchar();
        while(!isdigit(ch)) ch=getchar();
        while(isdigit(ch)) x=x*10+ch-'0',ch=getchar();
        return x*w;
}
void update(int x) {
        if(t[x<<1].val>=t[x<<1|1].val) {
                t[x].val=t[x<<1].val,t[x].idx=t[x<<1].idx;
        }
        else {
                t[x].val=t[x<<1|1].val,t[x].idx=t[x<<1|1].idx;
        }
        return ;
}

void build(int x,int l,int r) {
        t[x].l=l;t[x].r=r;
        if(l==r) {
                t[x].idx=l;t[x].val=a[l];
                return ;
        }
        int mid=(l+r)>>1;
        build(x<<1,l,mid);
        build(x<<1|1,mid+1,r);
        update(x);
}
tree query(int x,int l,int r) {
        if(l<=t[x].l && t[x].r<=r) return t[x];
        int mid=(t[x].l+t[x].r)>>1;
        tree ans1,ans2;
        bool flag1=false,flag2=false;
        if(l<=mid) {ans1=query(x<<1,l,r);flag1=1;}
        if(r>mid) {ans2=query(x<<1|1,l,r);flag2=1;}
        if(flag1 && flag2) return (ans1.val>ans2.val)?ans1:ans2;
        return (flag1)?ans1:ans2;
}
int main() {
        n=read();int res;
        for (int i=1;i<n;++i) a[i]=read();
        build(1,1,n);
        for (int i=n-1;i>=1;--i) {
                if(a[i]==n) {dp[i]=(long long)(n-i);continue;}
                res=query(1,i+1,a[i]).idx;
                dp[i]=1ll*(n-i+dp[res]-a[i]+res);
        }
        for (int i=1;i<=n;++i) ans+=dp[i];
        printf("%lld\n",ans);
        return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+5;
typedef long long LL;
int readint(){
    int x=0,f=1;char s=getchar();
    #define sc (s=getchar())
    while(s<'0'||s>'9'){
        if(s=='-')
            f=-1;
        sc;
    }
    while(s>='0'&&s<='9'){
        x=(x<<3)+(x<<1)+(s^48);
        sc;
    }
    #undef sc
    return x*f;
}
int a[maxn];
LL dp[maxn];
vector<int> vec;
int main (){
    int n=readint();
    for(int i=1;i<n;i++){
        a[i]=readint();
    }
    dp[n-1]=1;
    int ans=dp[n-1];
    vec.push_back(n-1);
    for(int i=n-2;i>=1;i--){
        int k=*(upper_bound(vec.rbegin(),vec.rend(),a[i])-1);
        dp[i]=0ll+dp[k]+(n-k)-(a[i]-k)+(k-i);
        ans+=dp[i];
        while(!vec.empty()&&a[i]>a[vec.back()])
            vec.pop_back();
        vec.push_back(i);
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N=200001;

int n,a[N],t[N];
ll ans,f[N];

void read(int &x) {scanf("%d",&x);}

int find(int i,int l,int r,int x,int y)
{
	if (l>=x&&r<=y) return t[i];
	int mid=(l+r)>>1;
	if (mid>=y) return find(i<<1,l,mid,x,y);
		else if (mid<x) return find(i<<1|1,mid+1,r,x,y);
			else
			{
				int u=find(i<<1,l,mid,x,y),v=find(i<<1|1,mid+1,r,x,y);
				if (a[u]>a[v]) return u;else return v;
			}
}

void build(int i,int l,int r)
{
	if (l==r) {t[i]=l;return;}
	int mid=(l+r)>>1,ls=i<<1,rs=ls|1;
	build(ls,l,mid);build(rs,mid+1,r);
	if (a[t[ls]]>a[t[rs]]) t[i]=t[ls];else t[i]=t[rs];
}

int main()
{
	read(n);
	for (int i=1;i<n;i++) read(a[i]);
	build(1,1,n);
	for (int i=n-1;i>=1;i--)
	{
		int k=find(1,1,n,i+1,a[i]);
		f[i]=f[k]+(n-i)-(a[i]-k);
	}
	for (int i=1;i<=n;i++) ans+=f[i];
	cout<<ans<<endl;
}


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
#include <math.h>
using namespace std;
const int N=100005;
int a[N];
long long dp[N];
int f[N][25],g[N][25];
int rmq(int l,int r){
	int s=log2((r-l+1)*1.0);
	if(f[l][s]>f[r-(1<<s)+1][s])return g[l][s];
	else return g[r-(1<<s)+1][s];
}
int main(){
//	freopen("trains.in","r",stdin);
//	freopen("trains.out","w",stdout);
	int n,i,j,k,o,p,l;
	scanf("%d",&n);
	for(i=1;i<n;i++){
	scanf("%d",&a[i]);
	g[i][0]=i,f[i][0]=a[i];
}
    g[n][0]=0,f[n][0]=n;
    for(j=1;(1<<j)<=n;j++)
    for(i=1;i<=n;i++)
	if(i+(1<<j)-1<=n){
	f[i][j]=max(f[i][j-1],f[i+(1<<j-1)][j-1]);
	if(f[i][j]==f[i][j-1])g[i][j]=g[i][j-1];
	else g[i][j]=g[i+(1<<j-1)][j-1];
}
    long long ans=0;
    for(i=n-1;i>=1;i--){
    	int pos=rmq(i+1,a[i]);
    	if(a[i]>=n)dp[i]=n-i;
    	else dp[i]=dp[pos]+(n-i)-(a[i]-pos);
    	ans+=dp[i];
    }
    printf("%lld",ans);
	return 0;
}


#include<bits/stdc++.h>
#define fint register int
#define h 5001
#define p 4570944
#define ls x<<1
#define rs x<<1|1
#define tt t[x].tot
#define ll t[x].l
#define rr t[x].r
#define int long long
using namespace std;
struct node
{
	int l;
	int r;
	int tot;
}
t[p];
int a[p],f[p],num[p];
inline int query(int x,int l,int r);
inline void build(int x,int l,int r);
signed main()
{
	int n;
	cin>>n;
	for(fint i=1;i<n;i++)
	cin>>a[i];
	for(fint i=1;i<=n;i++)
	num[i]=i;
	build(1,1,n);
	int ans=0;
	for(fint i=n-1;i>=1;i--)
	{
		int M=query(1,i+1,a[i]);
		if(a[i]>=n)
		f[i]=n-i;
		else
		f[i]=f[M]+(n-i)-(a[i]-M);
		ans+=f[i];
	}
	cout<<ans;
	return 0;
}

inline void build(int x,int l,int r)
{
	ll=l,rr=r;
	if(l==r)
	{
		tt=num[l];
		return ;
	}
	int mid=(rr+ll)>>1;
	build(ls,l,mid);
	build(rs,mid+1,r);
	tt=max(t[ls].tot,t[rs].tot);
	return ;
}

inline int query(int x,int l,int r)
{
	if(ll>=l&&rr<=r)
	return tt;
	int mid=(ll+rr)>>1;
	int a=0,b=0;
	if(l<=mid)
	a=query(ls,l,r);
	if(r>mid)
	b=query(rs,l,r);
	return max(a,b);
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
using namespace std;

#define M 100005
#define LL long long

int n, a[M];
LL ans, f[M];
vector <int> s;

int main (){
    scanf ("%d", &n);
    for (int i = 1; i < n; i ++)
        scanf ("%d", &a[i]);
    f[n - 1] = ans = 1;
    s.push_back (n - 1);
    for (int i = n - 2; i >= 1; i --){
        int k = *(upper_bound (s.rbegin (), s.rend (), a[i]) - 1);
        f[i] = f[k] + (k - i) + (n - a[i]);
        ans += f[i];
        while (! s.empty () && a[i] >= a[s.back ()])
            s.pop_back ();
        s.push_back (i);
    }
    printf ("%lld\n", ans);
    return 0;
}


//C++ Template

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef vector<LL> VI;
typedef vector<VI> VVI;
typedef map<LL,LL>::iterator ITmii;

#define EPS 1e-9
#define FOR1(x,y,z) for( LL x = y; x < z ; ++x)
#define FOR(x,y) FOR1(x,0,y)
#define FOR2(x,y) for(LL x = y; x >= 0; --x)
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define SORT(X) sort(ALL(X))
#define SIZE(X) ((LL)(X).size())

//------------------------------------------------------------------
VI s, a;
LL ans;
const LL INF = 1e16;
LL n;

LL find(LL id, LL l, LL r, LL x, LL y){
	LL mid = (l+r)/2;
	if(l >=x and r <= y) return s[id];
	else if(l > y or r < x) return INF;
	else return min(find(2*id,l,mid,x,y),find(2*id+1,mid+1,r,x,y));
}

void build(LL id,LL l, LL r){ //inicializado a 1, 0, n
	if(l == r){
		if(l == n-1) s[id]=0;
		else {
			s[id] = n - a[l] - l - 1 + find(1,0,n-1,l+1,l+a[l]);
			ans += s[id];
		}
		return;
	}
	LL mid = (l+r)/2;
	build(2*id+1,mid+1,r);
	build(2*id ,l,mid);
	s[id] = min(s[2*id],s[2*id+1]);
}

int main(){

	cin >> n;
	ans = (n*(n-1))/2;
	a = VI(n);
	s = VI(4*n,INF);
	FOR(i,n-1){
		LL x;
		cin >> x;
		--x;
		a[i] = x-i;
	}
	build(1,0,n-1);
	cout << ans << '\n';
}


//C++ Template

#include <bits/stdc++.h>
using namespace std;

typedef vector<LL> VI;
typedef vector<VI> VVI;
typedef long long LL;
typedef map<LL,LL>::iterator ITmii;

#define EPS 1e-9
#define FOR1(x,y,z) for( LL x = y; x < z ; ++x)
#define FOR(x,y) FOR1(x,0,y)
#define FOR2(x,y) for(LL x = y; x >= 0; --x)
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define SORT(X) sort(ALL(X))
#define SIZE(X) ((LL)(X).size())

//------------------------------------------------------------------
VI s, a;
LL ans;
const LL INF = 1e7;
LL n;

LL find(LL id, LL l, LL r, LL x, LL y){
	LL mid = (l+r)/2;
	if(l >=x and r <= y) return s[id];
	else if(l > y or r < x) return INF;
	else return min(find(2*id,l,mid,x,y),find(2*id+1,mid+1,r,x,y));
}

void build(LL id,LL l, LL r){ //inicializado a 1, 0, n
	if(l == r){
		if(l == n-1) s[id]=0;
		else {
			s[id] = n - a[l] - l - 1 + find(1,0,n-1,l+1,l+a[l]);
			ans += s[id];
		}
		return;
	}
	LL mid = (l+r)/2;
	build(2*id+1,mid+1,r);
	build(2*id ,l,mid);
	s[id] = min(s[2*id],s[2*id+1]);
}

int main(){

	cin >> n;
	ans = (n*(n-1))/2;
	a = VI(n);
	s = VI(4*n,INF);
	FOR(i,n-1){
		LL x;
		cin >> x;
		--x;
		a[i] = x-i;
	}
	build(1,0,n-1);
	cout << ans << '\n';
}


#include <bits/stdc++.h>
using namespace std;

vector < int > v;
vector < int > dp;
vector < int > seg;

void build (int left, int dreta, int node) {
    if (left == dreta) {
        seg[node] = v[left];
        return;
    }
    build (left, (dreta + left)/2, 2*node);
    build ((dreta + left)/2 + 1, dreta, 2*node + 1);
    seg[node] = max (seg[2*node], seg[2*node + 1]);
}

int busca (int a, int b, int x, int y, int node) {
    if (x >= a and y <= b) return seg[node];
    if (y < a or x > b) return 0;
    int h = busca (a, (a+b)/2, x, y, 2*node);
    int m = busca ((a+b)/2 + 1, b, x, y, 2*node + 1);
    if (v[h] > v[m]){
        return h;
    }
    return m;
}

int main () {
    int n;
    cin >> n;
    v = vector < int > (n-1);
    seg = vector < int > (4*n);
    dp = vector < int > (n-1);
    for (int i = 0; i < n-1; i++) {
        cin >> v[i];
        v[i]--;
    }
    build (0, n-2, 1);
    cout << "tumatr" << endl;
    dp[n-2] = 1;
    long long res = 1;
    for (int i = n-3; i >= 0; i--) {
        cout << "tumatr       " << i << endl;
        dp[i] = n-i+dp[busca(0, n-2, i+1, v[i],1)];
        res += dp[i];
    }
    cout << res << endl;

}


//C++ Template

#include <bits/stdc++.h>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
typedef map<int,int>::iterator ITmii;

#define EPS 1e-9
#define FOR1(x,y,z) for( int x = y; x < z ; ++x)
#define FOR(x,y) FOR1(x,0,y)
#define FOR2(x,y) for(int x = y; x >= 0; --x)
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define SORT(X) sort(ALL(X))
#define SIZE(X) ((int)(X).size())

//------------------------------------------------------------------
VI s, a;
LL ans;
const int INF = 1e7;
int n;

int find(int id, int l, int r, int x, int y){
	int mid = (l+r)/2;
	if(l >=x and r <= y) return s[id];
	else if(l > y or r < x) return INF;
	else return min(find(2*id,l,mid,x,y),find(2*id+1,mid+1,r,x,y));
}

void build(int id,int l, int r){ //inicializado a 1, 0, n
	if(l == r){
		if(l == n-1) s[id]=0;
		else {
			s[id] = n - a[l] - l - 1 + find(1,0,n-1,l+1,l+a[l]);
			ans += s[id];
		}
		return;
	}
	int mid = (l+r)/2;
	build(2*id+1,mid+1,r);
	build(2*id ,l,mid);
	s[id] = min(s[2*id],s[2*id+1]);
}

int main(){

	cin >> n;
	ans = (n*(n-1))/2;
	a = VI(n);
	s = VI(4*n,INF);
	FOR(i,n-1){
		int x;
		cin >> x;
		--x;
		a[i] = x-i;
	}
	build(1,0,n-1);
	cout << ans << '\n';
}


#include <bits/stdc++.h>

using namespace std;

int main() {

    set<int> numbers;
    map<int, int> left;
    map<int, int> right;

    int n, v;
    cin >> n >> v;
    numbers.insert(v);
    for (int i = 0; i < n - 1; i++) {
        cin >> v;
        auto it = numbers.upper_bound(v);
        int result;
        if (it != numbers.end() && left.count(*it) == 0) {
            left[*it] = v;
            result = *it;
        } else {
            it--;
            right[*it] = v;
            result = *it;
        }
        numbers.insert(v);
        cout << result;
        if (i == n - 2) cout << '\n';
        else cout << ' ';
    }
}


//C++ Template

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef vector<LL> VI;
typedef vector<VI> VVI;
typedef map<LL,LL>::iterator ITmii;

#define EPS 1e-9
#define FOR1(x,y,z) for( LL x = y; x < z ; ++x)
#define FOR(x,y) FOR1(x,0,y)
#define FOR2(x,y) for(LL x = y; x >= 0; --x)
#define PB push_back
#define ALL(X) (X).begin(), (X).end()
#define SORT(X) sort(ALL(X))
#define SIZE(X) ((LL)(X).size())

//------------------------------------------------------------------
VI s, a;
LL ans;
const LL INF = 1e7;
LL n;

LL find(LL id, LL l, LL r, LL x, LL y){
	LL mid = (l+r)/2;
	if(l >=x and r <= y) return s[id];
	else if(l > y or r < x) return INF;
	else return min(find(2*id,l,mid,x,y),find(2*id+1,mid+1,r,x,y));
}

void build(LL id,LL l, LL r){ //inicializado a 1, 0, n
	if(l == r){
		if(l == n-1) s[id]=0;
		else {
			s[id] = n - a[l] - l - 1 + find(1,0,n-1,l+1,l+a[l]);
			ans += s[id];
		}
		return;
	}
	LL mid = (l+r)/2;
	build(2*id+1,mid+1,r);
	build(2*id ,l,mid);
	s[id] = min(s[2*id],s[2*id+1]);
}

int main(){

	cin >> n;
	ans = (n*(n-1))/2;
	a = VI(n);
	s = VI(4*n,INF);
	FOR(i,n-1){
		LL x;
		cin >> x;
		--x;
		a[i] = x-i;
	}
	build(1,0,n-1);
	cout << ans << '\n';
}


/*
    Author: zxy_hhhh
    date: 2019/04/01
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#define rep(x,a,b) for (int x=int(a);x<=(int)(b);x++)
#define drp(x,a,b) for (int x=int(a);x>=(int)(b);x--)
#define cross(x,a) for (int x=hd[a];x;x=nx[x])
#define ll long long
using namespace std;
inline ll rd()
{
    ll _x=0;int _ch=getchar(),_f=1;
    for(;!isdigit(_ch)&&(_ch!='-')&&(_ch!=EOF);_ch=getchar());
    if (_ch=='-'){_f=0;_ch=getchar();}
    for(;isdigit(_ch);_ch=getchar()) _x=_x*10+_ch-'0';
    return _f?_x:-_x;
}
void write(ll _x){if (_x>=10) write(_x/10),putchar(_x%10+'0'); else putchar(_x+'0'); }
inline void wrt(ll _x,char _p){if (_x<0) putchar('-'),_x=-_x; write(_x); if (_p) putchar(_p);}
#define maxn 100005
#define inf 23333333333333333
int n,a[maxn];
ll mn[maxn<<2];
void update(int pos,int l,int r,int x,ll v) {
    if (l==r) {
        mn[pos]=v;
        return ;
    }
    int mid=(l+r)>>1;
    if (x<=mid) update(pos<<1,l,mid,x,v);
    else update(pos<<1|1,mid+1,r,x,v);
    mn[pos]=min(mn[pos<<1],mn[pos<<1|1]);
}
ll query(int pos,int l,int r,int ql,int qr) {
    if (r<ql||l>qr) return inf;
    if (ql<=l&&r<=qr) return mn[pos];
    int mid=(l+r)>>1;
    return min(query(pos<<1,l,mid,ql,qr),query(pos<<1|1,mid+1,r,ql,qr));
}
int main()
{
    n=rd();
    rep(i,1,n-1) a[i]=rd();
    ll ans=0;
    update(1,1,n,n,n);
    drp(i,n-1,1) {
        ll v=query(1,1,n,i+1,a[i])-a[i]+n-i;
        update(1,1,n,i,v+i);
        ans+=v;
        wrt(v,'\n');
    }
    wrt(ans,'\n');
}


/*
    Author: zxy_hhhh
    date: 2019/04/01
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#define rep(x,a,b) for (int x=int(a);x<=(int)(b);x++)
#define drp(x,a,b) for (int x=int(a);x>=(int)(b);x--)
#define cross(x,a) for (int x=hd[a];x;x=nx[x])
#define ll long long
using namespace std;
inline ll rd()
{
    ll _x=0;int _ch=getchar(),_f=1;
    for(;!isdigit(_ch)&&(_ch!='-')&&(_ch!=EOF);_ch=getchar());
    if (_ch=='-'){_f=0;_ch=getchar();}
    for(;isdigit(_ch);_ch=getchar()) _x=_x*10+_ch-'0';
    return _f?_x:-_x;
}
void write(ll _x){if (_x>=10) write(_x/10),putchar(_x%10+'0'); else putchar(_x+'0'); }
inline void wrt(ll _x,char _p){if (_x<0) putchar('-'),_x=-_x; write(_x); if (_p) putchar(_p);}
#define maxn 100005
#define inf 23333333333333333
int n,a[maxn];
pair<ll,int> mn[maxn<<2];
void update(int pos,int l,int r,int x,ll v) {
    if (l==r) {
        mn[pos]=make_pair(v,l);
        return ;
    }
    int mid=(l+r)>>1;
    if (x<=mid) update(pos<<1,l,mid,x,v);
    else update(pos<<1|1,mid+1,r,x,v);
    mn[pos]=min(mn[pos<<1],mn[pos<<1|1]);
}
pair<ll,int> query(int pos,int l,int r,int ql,int qr) {
    if (r<ql||l>qr) return make_pair(inf,n+1);
    if (ql<=l&&r<=qr) return mn[pos];
    int mid=(l+r)>>1;
    return min(query(pos<<1,l,mid,ql,qr),query(pos<<1|1,mid+1,r,ql,qr));
}
int main()
{
    n=rd();
    rep(i,1,n-1) a[i]=rd();
    ll ans=0;
    update(1,1,n,n,0);
    drp(i,n-1,1) {
        pair<ll,int> v=query(1,1,n,i+1,a[i]);
        ll res=v.first+n-i-(a[i]-v.second);
        update(1,1,n,i,res);
        ans+=res;
    }
    wrt(ans,'\n');
}


/*
    Author: zxy_hhhh
    date: 2019/04/01
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#define rep(x,a,b) for (int x=int(a);x<=(int)(b);x++)
#define drp(x,a,b) for (int x=int(a);x>=(int)(b);x--)
#define cross(x,a) for (int x=hd[a];x;x=nx[x])
#define ll long long
using namespace std;
inline ll rd()
{
    ll _x=0;int _ch=getchar(),_f=1;
    for(;!isdigit(_ch)&&(_ch!='-')&&(_ch!=EOF);_ch=getchar());
    if (_ch=='-'){_f=0;_ch=getchar();}
    for(;isdigit(_ch);_ch=getchar()) _x=_x*10+_ch-'0';
    return _f?_x:-_x;
}
void write(ll _x){if (_x>=10) write(_x/10),putchar(_x%10+'0'); else putchar(_x+'0'); }
inline void wrt(ll _x,char _p){if (_x<0) putchar('-'),_x=-_x; write(_x); if (_p) putchar(_p);}
#define maxn 100005
#define inf 23333333333333333
int n,a[maxn];
ll mn[maxn<<2];
void update(int pos,int l,int r,int x,ll v) {
    if (l==r) {
        mn[pos]=v;
        return ;
    }
    int mid=(l+r)>>1;
    if (x<=mid) update(pos<<1,l,mid,x,v);
    else update(pos<<1|1,mid+1,r,x,v);
    mn[pos]=min(mn[pos<<1],mn[pos<<1|1]);
}
ll query(int pos,int l,int r,int ql,int qr) {
    if (r<ql||l>qr) return inf;
    if (ql<=l&&r<=qr) return mn[pos];
    int mid=(l+r)>>1;
    return min(query(pos<<1,l,mid,ql,qr),query(pos<<1|1,mid+1,r,ql,qr));
}
int main()
{
    n=rd();
    rep(i,1,n-1) a[i]=rd();
    ll ans=0;
    update(1,1,n,n,n);
    drp(i,n-1,1) {
        ll v=query(1,1,n,i+1,a[i])-a[i]+n-i;
        update(1,1,n,i,v+i);
        ans+=v;
    }
    wrt(ans,'\n');
}


/*
    Author: zxy_hhhh
    date: 2019/04/01
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<set>
#include<vector>
#include<map>
#define rep(x,a,b) for (int x=int(a);x<=(int)(b);x++)
#define drp(x,a,b) for (int x=int(a);x>=(int)(b);x--)
#define cross(x,a) for (int x=hd[a];x;x=nx[x])
#define ll long long
using namespace std;
inline ll rd()
{
    ll _x=0;int _ch=getchar(),_f=1;
    for(;!isdigit(_ch)&&(_ch!='-')&&(_ch!=EOF);_ch=getchar());
    if (_ch=='-'){_f=0;_ch=getchar();}
    for(;isdigit(_ch);_ch=getchar()) _x=_x*10+_ch-'0';
    return _f?_x:-_x;
}
void write(ll _x){if (_x>=10) write(_x/10),putchar(_x%10+'0'); else putchar(_x+'0'); }
inline void wrt(ll _x,char _p){if (_x<0) putchar('-'),_x=-_x; write(_x); if (_p) putchar(_p);}
#define maxn 100005
#define inf 23333333333333333
int n,a[maxn];
ll mn[maxn<<2];
void update(int pos,int l,int r,int x,ll v) {
    if (l==r) {
        mn[pos]=v;
        return ;
    }
    int mid=(l+r)>>1;
    if (x<=mid) update(pos<<1,l,mid,x,v);
    else update(pos<<1|1,mid+1,r,x,v);
    mn[pos]=min(mn[pos<<1],mn[pos<<1|1]);
}
ll query(int pos,int l,int r,int ql,int qr) {
    if (r<ql||l>qr) return inf;
    if (ql<=l&&r<=qr) return mn[pos];
    int mid=(l+r)>>1;
    return min(query(pos<<1,l,mid,ql,qr),query(pos<<1|1,mid+1,r,ql,qr));
}
int main()
{
    n=rd();
    rep(i,1,n-1) a[i]=rd();
    ll ans=0;
    drp(i,n-1,1) {
        ll v=query(1,1,n,i+1,a[i]);
        update(1,1,n,i,v+n-i);
        ans+=v+n-i;
    }
    wrt(ans,'\n');
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 1;
int lg[N];
int sparseTable[N][20];
int arr[N];
int n;

inline void build() {
	for (int i = 0; i < n;) {
		sparseTable[i][0] = i++;
		lg[i] = lg[i - 1] + (i == (i & -i));
	}

	for (int j = 1; (1 << j) <= n; ++j)
		for (int i = 0; i + (1 << j) <= n; ++i) {
			int a = sparseTable[i][j - 1], b = sparseTable[i + (1 << (j - 1))][j - 1];
			sparseTable[i][j] = (arr[a] > arr[b] ? a : b);
		}

}

inline int getMax(int i, int j) {
	int l = lg[j - i + 1];
	int a = sparseTable[i][l], b = sparseTable[j - (1 << l) + 1][l];
	return (arr[a] > arr[b] ? a : b);
}

int dp[N];

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 0; i + 1 < n; ++i)
		cin >> arr[i], --arr[i];

	build();

	long long res = 0;
	for (int i = n - 2; i >= 0; --i) {
		if (arr[i] == n - 1)
			dp[i] = n - i - 1;
		else {
			int ind = getMax(i + 1, arr[i]);
			dp[i] = n - 1 - arr[i] + dp[ind] + ind - i;
		}
		res += dp[i];
	}

	cout << res;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 1;
int lg[N];
int sparseTable[N][(int) ceil(log2(N)) + 1];
int arr[N];
int n;

inline void build() {
	for (int i = 0; i < n;) {
		sparseTable[i][0] = i++;
		lg[i] = lg[i - 1] + (i == (i & -i));
	}

	for (int j = 1; (1 << j) <= n; ++j)
		for (int i = 0; i + (1 << j) <= n; ++i) {
			int a = sparseTable[i][j - 1], b = sparseTable[i + (1 << (j - 1))][j - 1];
			sparseTable[i][j] = (arr[a] > arr[b] ? a : b);
		}

}

inline int getMax(int i, int j) {
	int l = lg[j - i + 1];
	int a = sparseTable[i][l], b = sparseTable[j - (1 << l) + 1][l];
	return (arr[a] > arr[b] ? a : b);
}
int dp[N];
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 0; i + 1 < n; ++i)
		cin >> arr[i], --arr[i];

	build();

	long long res = 0;
	for (int i = n - 2; i >= 0; --i) {
		if (arr[i] == n - 1)
			dp[i] = n - i - 1;
		else
			dp[i] = n - arr[i] + dp[getMax(i + 1, arr[i])];
		res += dp[i];
	}

	cout << res;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 1;
int lg[N];
int sparseTable[N][20];
int arr[N];
int n;

inline void build() {
	lg[0] = -1 ;
	for (int i = 0; i < n;) {
		sparseTable[i][0] = i;
		++i;
		lg[i] = lg[i - 1] + (i == (i & -i));
	}

	for (int j = 1; (1 << j) <= n; ++j)
		for (int i = 0; i + (1 << j) <= n; ++i) {
			int a = sparseTable[i][j - 1], b = sparseTable[i + (1 << (j - 1))][j - 1];
			sparseTable[i][j] = (arr[a] > arr[b] ? a : b);
		}

}

inline int getMax(int i, int j) {
	int l = lg[j - i + 1];
	int a = sparseTable[i][l], b = sparseTable[j - (1 << l) + 1][l];
	return (arr[a] > arr[b] ? a : b);
}

int dp[N];

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 0; i + 1 < n; ++i)
		cin >> arr[i], --arr[i];

	build();

	long long res = 0;
	for (int i = n - 2; i >= 0; --i) {
		if (arr[i] == n - 1)
			dp[i] = n - i - 1;
		else {
			int ind = getMax(i + 1, arr[i]);
			dp[i] = n - 1 - arr[i] + dp[ind] + ind - i;
		}

		res += dp[i];
	}

	cout << res;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 1;
int lg[N];
int sparseTable[N][(int) ceil(log2(N)) + 1];
int arr[N];
int n;

inline void build() {
	for (int i = 0; i < n;) {
		sparseTable[i][0] = i++;
		lg[i] = lg[i - 1] + (i == (i & -i));
	}

	for (int j = 1; (1 << j) <= n; ++j)
		for (int i = 0; i + (1 << j) <= n; ++i) {
			int a = sparseTable[i][j - 1], b = sparseTable[i + (1 << (j - 1))][j - 1];
			sparseTable[i][j] = (arr[a] > arr[b] ? a : b);
		}

}

inline int getMax(int i, int j) {
	int l = lg[j - i + 1];
	int a = sparseTable[i][l], b = sparseTable[j - (1 << l) + 1][l];
	return (arr[a] > arr[b] ? a : b);
}

int dp[N];

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 0; i + 1 < n; ++i)
		cin >> arr[i], --arr[i];

	build();

	long long res = 0;
	for (int i = n - 2; i >= 0; --i) {
		if (arr[i] == n - 1)
			dp[i] = n - i - 1;
		else {
			int ind = getMax(i + 1, arr[i]);
			dp[i] = n - 1 - arr[i] + dp[ind] + ind - i;
		}
		res += dp[i];
	}

	cout << res;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 1;
int lg[N];
int sparseTable[N][20];
int arr[N];
int n;

inline void build() {
	lg[0] = -1 ;
	for (int i = 0; i < n;) {
		sparseTable[i][0] = i;
		++i;
		lg[i] = lg[i - 1] + (i == (i & -i));
	}

	for (int j = 1; (1 << j) <= n; ++j)
		for (int i = 0; i + (1 << j) <= n; ++i) {
			int a = sparseTable[i][j - 1], b = sparseTable[i + (1 << (j - 1))][j - 1];
			sparseTable[i][j] = (arr[a] > arr[b] ? a : b);
		}

}

inline int getMax(int i, int j) {
	int l = lg[j - i + 1];
	int a = sparseTable[i][l], b = sparseTable[j - (1 << l) + 1][l];
	return (arr[a] > arr[b] ? a : b);
}

long long dp[N];

int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 0; i + 1 < n; ++i)
		cin >> arr[i], --arr[i];

	build();

	long long res = 0;
	for (int i = n - 2; i >= 0; --i) {
		if (arr[i] == n - 1)
			dp[i] = n - i - 1;
		else {
			int ind = getMax(i + 1, arr[i]);
			dp[i] = n - 1 - arr[i] + dp[ind] + ind - i;
		}

		res += dp[i];
	}

	cout << res;
}


#include <bits/stdc++.h>
using namespace std;

const int N = 1e5 + 1;
int lg[N];
int sparseTable[N][20];
int arr[N];
int n;

inline void build() {
	for (int i = 0; i < n;) {
		sparseTable[i][0] = i++;
		lg[i] = lg[i - 1] + (i == (i & -i));
	}

	for (int j = 1; (1 << j) <= n; ++j)
		for (int i = 0; i + (1 << j) <= n; ++i) {
			int a = sparseTable[i][j - 1], b = sparseTable[i + (1 << (j - 1))][j - 1];
			sparseTable[i][j] = (arr[a] > arr[b] ? a : b);
		}

}

inline int getMax(int i, int j) {
	int l = lg[j - i + 1];
	int a = sparseTable[i][l], b = sparseTable[j - (1 << l) + 1][l];
	return (arr[a] > arr[b] ? a : b);
}
int dp[N];
int main() {
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	cin >> n;
	for (int i = 0; i + 1 < n; ++i)
		cin >> arr[i], --arr[i];

	build();

	long long res = 0;
	for (int i = n - 2; i >= 0; --i) {
		if (arr[i] == n - 1)
			dp[i] = n - i - 1;
		else
			dp[i] = n - arr[i] + dp[getMax(i + 1, arr[i])];
		res += dp[i];
	}

	cout << res;
}


#include <algorithm>
#include <cstdio>

#define CHECK(condition) if (!(condition)) { \
  printf("CHECK(%s) failed\n", #condition); \
  exit(0); \
}

typedef long long int64;

using namespace std;

int n;
int a[100002];
int loga[100003];
int64 dp[100002];

int main() {
  CHECK(scanf("%d", &n));
  for (int i = 0; i < n - 1; ++i) {
    CHECK(scanf("%d", &a[i]));
    --a[i];
  }
  a[n - 1] = n - 1;
  loga[n] = n - 1;
  dp[n - 1] = 0;

  int64 ret = 0;
  for (int i = n - 2; i >= 0; --i) {
    int x = i + 1;
    for (int j = a[i] + 1; j > 0; j -= j & -j) {
      if (a[loga[j]] > a[x]) x = loga[j];
    }
    for (int j = i + 1; j <= n; j += j & -j) {
      if (a[i] > a[loga[j]]) loga[j] = i;
    }
    dp[i] = dp[x] + n - i - 1 - (a[i] - x);
    ret += dp[i];
  }
  printf("%lld\n", ret);

  return 0;
}


#include <algorithm>
#include <cstdio>
#include <set>

#define CHECK(condition) if (!(condition)) { \
  printf("CHECK(%s) failed\n", #condition); \
  exit(0); \
}

typedef long long int64;

using namespace std;

int n;
int a[100000];

int main() {
  CHECK(scanf("%d", &n));
  for (int i = 0; i < n - 1; ++i) CHECK(scanf("%d", &a[i]));

  set<int> stair;
  int64 ret = 0;
  int64 curr = 0;
  for (int i = n - 1; i >= 0; --i) {
    curr += stair.size();
    set<int>::iterator it = stair.upper_bound(a[i]);
    while (it != stair.begin()) {
      set<int>::iterator jt = it;
      --jt;
      curr -= (*jt - i);
      stair.erase(jt);
    }
    curr += (a[i] - i);
    stair.insert(a[i]);
    ret += curr;
  }
  printf("%lld\n", ret);

  return 0;
}


#include <algorithm>
#include <cstdio>

#define CHECK(condition) if (!(condition)) { \
  printf("CHECK(%s) failed\n", #condition); \
  exit(0); \
}

typedef long long int64;

using namespace std;

int n;
int a[100001];
int loga[100001];
int64 dp[100001];

int main() {
  CHECK(scanf("%d", &n));
  a[0] = 0;
  for (int i = 1; i < n; ++i) {
    CHECK(scanf("%d", &a[i]));
  }
  a[n] = n;
  loga[n] = n;
  dp[n] = 0;

  int64 ret = 0;
  for (int i = n - 1; i > 0; --i) {
    int x = i + 1;
    for (int j = a[i]; j > 0; j -= j & -j) {
      if (a[loga[j]] > a[x]) x = loga[j];
    }
    for (int j = i; j <= n; j += j & -j) {
      if (a[i] > a[loga[j]]) loga[j] = i;
    }
    dp[i] = dp[x] + n - i - (a[i] - x);
    ret += dp[i];
  }
  printf("%lld\n", ret);

  return 0;
}


#include <algorithm>
#include <cstdio>
#include <set>

#define CHECK(condition) if (!(condition)) { \
  printf("CHECK(%s) failed\n", #condition); \
  exit(0); \
}

typedef long long int64;

using namespace std;

int n;
int a[100000];

int main() {
  CHECK(scanf("%d", &n));
  for (int i = 0; i < n - 1; ++i) {
    CHECK(scanf("%d", &a[i]));
    --a[i];
  }

  set<int> stair;
  int64 ret = 0;
  int64 curr = 0;
  for (int i = n - 2; i >= 0; --i) {
    set<int>::iterator it = stair.upper_bound(a[i]);
    while (it != stair.begin()) {
      set<int>::iterator jt = it;
      --jt;
      curr -= n - *jt - 1;
      stair.erase(jt);
    }
    curr += n - a[i] - 1;
    stair.insert(a[i]);
    ret += n - i - 1 + curr;
  }
  printf("%lld\n", ret);

  return 0;
}


#include <algorithm>
#include <cstdio>
#include <stack>

#define CHECK(condition) if (!(condition)) { \
  printf("CHECK(%s) failed\n", #condition); \
  exit(0); \
}

typedef long long int64;

using namespace std;

int n;
int a[100000];

int main() {
  CHECK(scanf("%d", &n));
  for (int i = 0; i < n - 1; ++i) {
    CHECK(scanf("%d", &a[i]));
    --a[i];
  }

  stack<int> stair;
  int64 ret = 0;
  int64 curr = 0;
  for (int i = n - 2; i >= 0; --i) {
    while (!stair.empty() && stair.top() <= a[i]) {
      curr -= n - stair.top() - 1;
      stair.pop();
    }
    curr += n - a[i] - 1;
    stair.push(a[i]);
    ret += curr + n - i - 1;
  }
  printf("%lld\n", ret);

  return 0;
}


#include <algorithm>
#include <cstdio>

#define CHECK(condition) if (!(condition)) { \
  printf("CHECK(%s) failed\n", #condition); \
  exit(0); \
}

typedef long long int64;

using namespace std;

int n;
int a[100000];
int64 dp[100000];
int loga[100001];

int main() {
  CHECK(scanf("%d", &n));
  for (int i = 0; i < n - 1; ++i) {
    CHECK(scanf("%d", &a[i]));
    --a[i];
  }
  a[n - 1] = n - 1;
  loga[n] = n - 1;

  int64 ret = 0;
  for (int i = n - 2; i >= 0; --i) {
    int x = i + 1;
    for (int j = a[i] + 1; j > 0; j -= j & -j) {
      if (a[loga[j]] > a[x]) x = loga[j];
    }
    for (int j = i + 1; j <= n; j += j & -j) {
      if (a[i] > a[loga[j]]) loga[j] = i;
    }
    dp[i] = dp[x] + n - i - 1 - (a[i] - x);
    ret += dp[i];
  }
  printf("%lld\n", ret);

  return 0;
}