#include <bits/stdc++.h>
using namespace std;
long long n;
long long f(long long x, long long y) {
  long long ans;
  if (x <= y)
    ans = y - x;
  else
    ans = n - x + y;
  return ans;
}
int main() {
  long long m;
  cin >> n >> m;
  long long a[m + 1];
  for (long long i = 1; i <= m; i++) {
    cin >> a[i];
  }
  long long curr = 1, ans = 0;
  for (long long i = 1; i <= m; i++) {
    ans += f(curr, a[i]);
    curr = a[i];
  }
  cout << ans << '\n';
  return 0;
}
