#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  long long int i, j, n, m, sum = 0;
  cin >> n >> m;
  long long int arr[m + 1];
  arr[0] = 1;
  for (i = 1; i < m + 1; i++) {
    cin >> arr[i];
  }
  for (i = 1; i < m + 1; i++) {
    if (arr[i - 1] < arr[i]) {
      sum += arr[i] - arr[i - 1];
    } else if (arr[i - 1] == arr[i]) {
      continue;
    } else {
      sum += (arr[i] - arr[0]) + (n - arr[i - 1]) + 1;
    }
  }
  cout << sum;
}
