Problem description

Xenia lives in a city that has n houses built along the main ringroad. The ringroad houses are numbered 1 through n in the clockwise order. The ringroad traffic is one way and also is clockwise.

Xenia has recently moved into the ringroad house number 1. As a result, she's got mthings to do. In order to complete the i-th task, she needs to be in the house number ai and complete all tasks with numbers less than i. Initially, Xenia is in the house number 1, find the minimum time she needs to complete all her tasks if moving from a house to a neighboring one along the ringroad takes one unit of time.

Input

The first line contains two integers n and m (2 ≤ n ≤ 105, 1 ≤ m ≤ 105). The second line contains m integers a1, a2, ..., am (1 ≤ ai ≤ n). Note that Xenia can have multiple consecutive tasks in one house.

Output

Print a single integer — the time Xenia needs to complete all tasks.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples

Input

4 3
3 2 3

Output

6

Input

4 3
2 3 3

Output

2

Note

In the first test example the sequence of Xenia's moves along the ringroad looks as follows: 1 → 2 → 3 → 4 → 1 → 2 → 3. This is optimal sequence. So, she needs 6 time units.

解题思路:如果后一个数b比前一个数a大,就累加它们的差值b-a;如果后一个数b比前一个数a小,就先循环一圈到后一个数b,此时累加数为n-a+b,注意:因为和最大值可能为1e10(11位)已经爆int,所以ans要用long long。水过!

AC代码:

 #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
int n,m;LL ans,a,b;
int main(){
cin>>n>>m;ans=,a=;
for(int i=;i<=m;++i){
cin>>b;
if(b>=a)ans+=b-a;
else ans+=n-a+b;
a=b;
}
cout<<ans<<endl;
return ;
}

最新文章

  1. php-fpm启动,重启,终止操作
  2. Java图片转换为base64格式
  3. 如何设置jvm内存
  4. js控制控件不可编辑
  5. java笔记--反射进阶之总结与详解
  6. git 项目初始化
  7. 定时器Timer
  8. python 安装预编译库注意事项-pip
  9. wxPython 基本框架与运行原理 -- App 与 Frame
  10. JAXB - Annotations, The Annotation XmlElement
  11. 锚点链接和hash属性
  12. zabbix_server表面启动成功,但是没有进程
  13. 黑科技:纯CSS定制文本省略
  14. 简单易学的SSM(Spring+SpringMVC+MyBatis)整合
  15. 匈牙利标记法定义ECMAScript变量前缀
  16. RSA解密解密
  17. CAT Caterpillar ET Diagnostic Adapter has a powerful function
  18. Tomcat内存优化
  19. MySQL锁详解!(转载)
  20. SharePoint 沙盒无法启动新的解决方案服务的SPUserCodeV4

热门文章

  1. 求n!(高精度问题)
  2. JavaScript学习笔记之DOM对象
  3. Mac在python3环境下安装virtualwrapper遇到的问题
  4. 使用Python PIL库中的Image.thumbnail函数裁剪图片
  5. 【5】Django项目配置settings.py详解
  6. 第三节:初识pandas之DataFrame(上)
  7. [加强版] Codeforces 835D Palindromic characteristics (回文自动机、DP)
  8. 【hiho一下 第144周】机会渺茫
  9. hdu_1005_Number Sequence_201310222120
  10. [bzoj1007][HNOI2008]水平可见直线_单调栈