#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, r = 0, x, p = 1;
  cin >> n >> m;
  while (m--) {
    cin >> x;
    r += (x - p + n) % n;
    p = x;
  }
  cout << r << endl;
}
