#include <bits/stdc++.h>   using namespace std;   using ll = long long;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int n, m;     cin >> n >> m;     int arr[m];     for (int i = 0; i < m; i++) cin >> arr[i];     long long res = arr[0];     for (int i = 1; i < m; i++) {       if (arr[i] - arr[i - 1] < 0) {         res += (n - arr[i - 1]) + arr[i];       } else         res += (arr[i] - arr[i - 1]);     }     cout << res - 1 <<   n ;     return 0;   }   