console.log((args=>{
  const [N,...A] = args.trim().split('\n').map(v=>v|0);
  if ( A[0] > 0 ) return '-1';
  let m = 0;
  for ( let i = 1; i < N; i++ ) m += Math.max( 1, A[i]-A[i-1] );
  return `${m}`;
})(require('fs').readFileSync('/dev/stdin', 'utf8')));
