program sequence_growing_easy
  implicit none
  integer :: n, a(200000), i
  integer(8) :: m
  a = 0
  read(*,*) n
  do i = 1, n
    read(*,*) a(i)
  end do
  if (a(1).ne.0) then
    write(*,'(i0)') -1
    stop
  end if
  do i = 1, n-1
    if (a(i+1).gt.a(i).and.a(i+1).ne.a(i)+1) then
      write(*,'(i0)') -1
      stop
    end if
  end do
  m = 0_8
  do i = 2, n
    if (a(i).gt.0) then
      m = m+1_8
      if (a(i).le.a(i-1)) m = m+int(a(i)-1,8)
    end if
  end do
  write(*,'(i0)') m
  stop
end program sequence_growing_easy