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