#include <bits/stdc++.h>
using namespace std;
long long int pow(long long int a, long long int b) {
  long long int ans = 1;
  while (b) {
    if (b & 1) ans *= a;
    a *= a;
    b >>= 1;
  }
  return ans;
}
long long int powmod(long long int a, long long int b) {
  long long int ans = 1;
  while (b) {
    if (b & 1) ans = (ans * a) % 10000000007;
    a = (a * a) % 10000000007;
    b >>= 1;
  }
  return ans;
}
bool ispal(string &a) {
  for (int i = 0; i < a.size() / 2; i++)
    if (a[i] != a[a.size() - i - 1]) return false;
  return true;
}
unsigned long long int sum(unsigned long long int n) {
  unsigned long long int x = 0;
  while (n) {
    x += n % 10;
    n /= 10;
  }
  return x;
}
int main() {
  {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
  };
  unsigned long long int n, m;
  cin >> n >> m;
  unsigned long long int ans = 0, p = 1;
  for (int i = 0; i < m; i++) {
    unsigned long long int x;
    cin >> x;
    ans += (p > x) ? n - p + x : x - p;
    p = x;
  }
  cout << ans << endl;
  return 0;
}
