#include <bits/stdc++.h>
using namespace std;
string cinline() {
  string res;
  getline(cin, res);
  return res;
}
int main() {
  cin.tie();
  cout.tie();
  int n, m;
  cin >> n >> m;
  n--;
  int curr = 0;
  long long ans = 0;
  for (int i = 0; i < m; i++) {
    int a;
    cin >> a;
    a--;
    if (curr > a) {
      ans += n + 1 - curr;
      curr = 0;
    }
    ans += a - curr;
    curr = a;
  }
  cout << ans;
  return 0;
}
