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