#include <bits/stdc++.h>
using namespace std;
int main() {
  int m, n, x, y;
  unsigned long long int cnt = 0 * 1ll;
  scanf("%d %d", &n, &m);
  x = 1;
  for (int i = 0; i < m; i++) {
    scanf("%d", &y);
    if (y > x)
      cnt += y - x;
    else if (y < x)
      cnt += n - x + y + 0ll;
    x = y;
  }
  cout << cnt << endl;
}
