#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  unsigned long long sum{};
  int cur{1};
  for (int i{}; i < m; i++) {
    int x;
    cin >> x;
    if (cur < x) {
      sum += x - cur;
      cur = x;
    } else if (cur > x) {
      sum += n - cur + x;
      cur = x;
    } else
      continue;
  }
  cout << sum;
}
