#include <bits/stdc++.h>
using namespace std;
int main(void) {
  ios::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  long long int in, h = 1;
  unsigned long long int ans = 0;
  while (m--) {
    cin >> in;
    if (in >= h)
      ans += in - h;
    else
      ans += (n - h) + in;
    h = in;
  }
  cout << ans;
  return 0;
}
