#include <bits/stdc++.h>
using namespace std;
int main() {
  int temp, n, m, j = 0;
  cin >> n >> m;
  vector<int> a(m);
  for (int i = 0; i < m; ++i) scanf("%d", &a[i]);
  long long ans = a[0] - 1;
  for (int i = 1; i < m; ++i) {
    if (a[i - 1] == a[i]) continue;
    if (a[i - 1] < a[i])
      ans += a[i] - a[i - 1];
    else
      ans += n - a[i - 1] + a[i];
  }
  printf("%I64d", ans);
  return 0;
}
