#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, a, b, k = 0;
  cin >> n >> m;
  cin >> a;
  int x = a;
  for (int i = 2; i <= m; ++i) {
    cin >> b;
    if (b >= a)
      k += b - a;
    else
      k += n - a + b;
    a = b;
  }
  k += x - 1;
  cout << k;
}
