#include<bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i) 
#define PI acos(-1)
#define pcnt __builtin_popcountll
#define rng(a) a.begin(), a.end()
#define sz(x) (int)(x).size()
#define v(T) vector<T>
#define vv(T) v(v(T))
#define fi first
#define se second

using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

template<typename T>inline istream& operator>>(istream&i,v(T)&v)
{rep(j,sz(v))i>>v[j];return i;}
template<typename T1,typename T2>inline istream& operator>>(istream&i,pair<T1,T2>&v)
{return i>>v.fi>>v.se;}
template<class T> inline bool chmax(T& a, T b) {if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) {if (a > b) { a = b; return true; } return false; }

ll INF = 1001001001;
ll LINF = 1001001001001001001ll;

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    ll n;
    cin>>n;
    vl as(n);
    cin>>as;

    if (n == 1) {
        if (as[0]) cout<<-1<<endl;
        else cout<<0<<endl;
        return 0;
    }

    ll ans = as.back();
    for (int i = n - 2; i >= 0; --i) {
        if (as[i] > i) {
            cout<<-1<<endl;
            return 0;
        }
        if (as[i] + 1 < as[i + 1]) {
            cout<<-1<<endl;
            return 0;
        }
        if (as[i] + 1 == as[i + 1]) continue;
        ans += as[i];
    }
    cout<<ans<<endl;
}
#include<bits/stdc++.h>
#define re register
using namespace std;
template <typename T>inline void read(T &x)
{
	x=0;int f=1;char c=getchar();
	for(;c>'9'||c<'0';c=getchar()) if(c=='-') f=-1;
	for(;c>='0'&&c<='9';c=getchar()) x=(x<<3)+(x<<1)+(c^48);
	x*=f;
}
const int maxn=1e5+5;
struct edge
{
	int to,nxt;
}e[maxn<<1];
int n,k,head[maxn],cnt;
int a[maxn],rem[maxn],ans;
void add(int u,int v)
{
	e[++cnt].to=v;
	e[cnt].nxt=head[u];
	head[u]=cnt;
}
void dfs(int u)
{
	for(re int i=head[u];i!=0;i=e[i].nxt)
	{
		int v=e[i].to;
		dfs(v);
		if(rem[v]==k-1&&u!=1) 
		{
			ans++;
			rem[v]=-1;
		}
		rem[u]=max(rem[u],rem[v]+1);
	}
}
int main()
{
	read(n),read(k);
	for(re int i=1;i<=n;++i)
		read(a[i]);
	if(a[1]!=1) ++ans;
	for(re int i=2;i<=n;++i)
		add(a[i],i);
	dfs(1);
	printf("%d",ans);
	return 0;
}