#include <bits/stdc++.h>
using namespace std;
int n, m;
int pos;
long long cnt;
int main() {
  cin >> n >> m;
  pos = 1;
  int a, d;
  for (int i = 1; i <= m; i++) {
    cin >> a;
    if (a == pos) {
      d = 0;
    } else if (a < pos) {
      d = a + n - pos;
    } else if (a > pos) {
      d = a - pos;
    }
    cnt += d;
    pos = a;
  }
  cout << cnt << endl;
}
