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