#include <bits/stdc++.h>
int main() {
  long long n, m, i, x, y, s = 0;
  scanf("%I64d%I64d", &n, &m);
  x = 1;
  for (i = 1; i <= m; ++i) {
    scanf("%I64d", &y);
    if (y > x)
      s += y - x;
    else if (x > y)
      s += n - x + y;
    x = y;
  }
  printf("%I64d\n", s);
  return 0;
}
