#include <bits/stdc++.h>
using namespace std;
int n, m;
int main() {
  while (scanf("%d %d", &n, &m) != EOF) {
    int last = 1, x;
    long long ret = 0;
    while (m--) {
      scanf("%d", &x);
      if (x >= last) {
        ret += x - last;
        last = x;
      } else {
        ret += n - last + x;
        last = x;
      }
    }
    printf("%I64d\n", ret);
  }
  return 0;
}
