#include <bits/stdc++.h>
using namespace std;
int ar[100005];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) scanf("%d", &ar[i]);
  long long ans = 0;
  int now = 1;
  for (int i = 0; i < m; i++) {
    if (ar[i] == now) continue;
    if (ar[i] > now) ans += ar[i] - now;
    if (ar[i] < now) ans += ar[i] + (n - now);
    now = ar[i];
  }
  printf("%I64d\n", ans);
  return 0;
}
