#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m;
  cin >> n >> m;
  vector<long long int> task(m);
  for (long long int i = 0; i < m; i++) {
    cin >> task[i];
  }
  long long int count = 0;
  for (long long int i = 0; i < m - 1; i++) {
    if (task[i + 1] < task[i]) {
      count++;
    }
  }
  long long int ans = count * n + task[m - 1] - 1;
  cout << ans;
  return 0;
}
