#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, m, max = 0;
  cin >> n >> m;
  int a[m];
  for (int i = 0; i < m; i++) cin >> a[i];
  int temp = 1;
  for (int i = 0; i < m; i++) {
    max = max + (a[i] - temp + n) % n;
    temp = a[i];
  }
  cout << max;
}
