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