#include <bits/stdc++.h>
using namespace std;
int main() {
  unsigned long long int n, m;
  cin >> n >> m;
  vector<unsigned long long int> a;
  a.push_back(1);
  for (int i = 0; i < m; i++) {
    unsigned long long int tmp;
    cin >> tmp;
    a.push_back(tmp);
  }
  unsigned long long int tym = 0;
  for (int x = 0; x < m; x++) {
    if (a[x] < a[x + 1])
      tym += (a[x + 1] - a[x]);
    else if (a[x] > a[x + 1])
      tym += (n - a[x] + a[x + 1]);
  }
  cout << tym;
}
