#include <bits/stdc++.h>
using namespace std;
int n, m, tmp;
long long ans = 0;
int main() {
  cin >> n >> m;
  int pos = 1;
  for (int i = 0; i < m; i++) {
    cin >> tmp;
    if (tmp == pos) continue;
    if (tmp > pos) {
      ans += (tmp - pos);
      pos = tmp;
      continue;
    }
    ans += n + 1 - pos;
    pos = 1;
    ans += (tmp - pos);
    pos = tmp;
  }
  cout << ans;
  return 0;
}
