#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, now;
  scanf("%d %d", &n, &m);
  long long res = 0;
  now = 1;
  while (m--) {
    int a;
    scanf("%d", &a);
    if (a >= now)
      res += (a - now);
    else
      res += (n - now) + a;
    now = a;
  }
  cout << res << "\n";
}
