#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m) {
    long long num = 0;
    int last = 0, now;
    while (m--) {
      cin >> now;
      if (last > now) num++;
      last = now;
    }
    cout << n * num + now - 1;
  }
}
