#include <bits/stdc++.h>
int main() {
  long long n, m, temp, i, j, r = 0, c = 0;
  long long rslt;
  long long tasks;
  scanf("%lli%lli", &n, &m);
  for (i = 0; i < m; i++) {
    scanf("%lli", &tasks);
    if (tasks < r) c++;
    r = tasks;
  }
  rslt = c * n + tasks - 1;
  printf("%lli", rslt);
}
