#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n = 0, m = 0;
  cin >> n >> m;
  int a[m];
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  long long int c = 0;
  c = a[0] - 1;
  for (int i = 1; i < m; i++) {
    if (a[i] >= a[i - 1]) {
      c += (a[i] - a[i - 1]);
    } else {
      c += (n - a[i - 1]);
      c += a[i];
    }
  }
  cout << c;
}
