#include <bits/stdc++.h>
using namespace std;
const int MAX = INT_MAX;
const int MIN = INT_MIN;
const int MOD = 1e9 + 7;
const int INF = numeric_limits<int>::max();
void stdinout() {}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m, x;
  cin >> n >> m;
  int pre = 1;
  long long l = 0;
  for (int i = 0; i < m; ++i) {
    cin >> x;
    l += (long long)((x - pre + n) % n);
    pre = x;
  }
  cout << l;
  return 0;
}
