#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, m, prev, cur, rez = 0;
  cin >> n >> m;
  cin >> prev;
  rez = prev - 1;
  for (int i = 1; i < m; i++) {
    cin >> cur;
    if (cur >= prev)
      rez += (cur - prev);
    else
      rez += (n - prev + cur);
    prev = cur;
  }
  cout << rez;
  return 0;
}
