#include <bits/stdc++.h>   using namespace std;   long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }   const int MAX = 1e5;   const long long MOD = 1e9 + 7;   const long long inf = 1e18;   int a[MAX + 5];   int main() {     ios_base::sync_with_stdio(false);     int n, m, r, i, j;     long long ans = 0;     cin >> n >> m;     a[0] = 1;     for ((i) = (1); (i) <= (m); ++i) cin >> a[i];     i = 0;     while (i < m) {       if (a[i + 1] > a[i])         ans += (a[i + 1] - a[i]);       else if (a[i + 1] < a[i])         ans += (n - a[i] + a[i + 1]);       i++;     }     cout << ans;   }   