#include <bits/stdc++.h>
using namespace std;
int n;
char ch;
void home() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
void enter() {
  int n, test;
  cin >> n >> test;
  int id = 1, x;
  long long res = 0;
  while (test--) {
    cin >> x;
    if (x >= id)
      res += x - id;
    else
      res += n - id + x;
    id = x;
  }
  cout << res;
}
int main() {
  home();
  enter();
}
