#include <stdio.h> int main() { int n, m; long long Count=0; scanf("%d%d", &n, &m); int cur, pre=1; while(m--) { scanf("%d", &cur); if(cur>pre) Count+=cur-pre; else if(cur<pre) Count+=(n-pre)+1+(cur-1); pre=cur; } printf("%I64d\n", Count); return 0; }