#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline")
#pragma GCC option("arch=native", "tune=native", "no-zero-upper")
#pragma GCC target("avx2")
long long read() {
  long long x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
using namespace std;
const int mod = 998244353;
const int mxn = 1e5 + 7;
const int inf = 1e9;
long long _, n, m, t, k, u, v, w, ans, cnt, ok, lim, len, tmp, last, nx;
struct node {
  int u, v, nx, w;
} e[mxn];
string str;
long long a[mxn];
vector<int> vc[mxn], s[mxn];
queue<int> q;
bool vis[mxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  while (cin >> n >> m) {
    ans = 0;
    tmp = 1;
    for (int i = 1; i <= m; i++) cin >> a[i];
    for (int i = 1; i <= m; i++) {
      if (a[i] > tmp)
        ans += a[i] - tmp, tmp = a[i];
      else if (a[i] < tmp)
        ans += n - tmp + a[i], tmp = a[i];
    }
    cout << ans << endl;
  }
}
void dfs(int x, int d) {
  while (!q.empty()) q.pop();
  q.push(x);
  vis[x] = true;
  ans = 0;
  while (!q.empty()) {
    int tmp = q.front();
    q.pop();
    ans++;
    for (int i = 0; i < vc[tmp].size(); i++) {
      if (!vis[vc[tmp][i]]) vis[vc[tmp][i]] = true, q.push(vc[tmp][i]);
    }
  }
  if ((ans - 2) & 1)
    cout << "Ashish" << endl;
  else
    cout << "Ayush" << endl;
}
bool isPrime(int num) {
  if (num == 1) return 0;
  if (num == 2 || num == 3) return 1;
  if (num % 6 != 1 && num % 6 != 5) return 0;
  int tmp = sqrt(num);
  for (int i = 5; i <= tmp; i += 6)
    if (num % i == 0 || num % (i + 2) == 0) return 0;
  return 1;
}
