#include <bits/stdc++.h>
int main() {
  long long int a = 1, b, n, m, t = 0;
  scanf("%lld %lld", &n, &m);
  while (m--) {
    scanf("%lld", &b);
    t += b >= a ? b - a : n - a + b;
    a = b;
  }
  printf("%lld", t);
}
