#include <bits/stdc++.h>
using namespace std;
const int N = 100005;
int a[N];
int main() {
  int n, m;
  while (scanf("%d %d", &n, &m) != EOF) {
    for (int i = 1; i <= m; ++i) scanf("%d", a + i);
    long long sum = 0;
    for (int i = 2; i <= m; ++i) {
      if (a[i] < a[i - 1]) sum++;
    }
    long long s = sum * n + a[m] - 1;
    cout << s << endl;
  }
  return 0;
}
