#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  int curr = 1;
  int next;
  long long step = 0;
  for (int i = 0; i < m; ++i) {
    scanf("%d", &next);
    if (next < curr) next += n;
    step += next - curr;
    curr = next == n ? n : next % n;
  }
  printf("%I64d\n", step);
}
