#define rep(i, n) for (int i = 0; i < (int)(n); i++) #define ALL(v) v.begin(), v.end() typedef long long ll; #include using namespace std; const ll INF=1LL<<60; struct Edge{ int to; ll w; Edge(int to,ll w) : to(to),w(w) {} }; using Graph=vector>; using pli=pair; template bool chmin(T& a,T b){ if(a>b){ a=b; return true; } return false; } int main(){ ios::sync_with_stdio(false); std::cin.tie(nullptr); int n,m; cin>>n>>m; vector A(m); rep(i,m){ cin>>A[i]; A[i]--; } Graph G(n*(m+1)); rep(j,n){ if(j0) G[j].push_back(Edge(j-1,1)); } rep(i,m){ rep(j,n){ if(j0) G[(i+1)*n+j].push_back(Edge(((i+1)*n+j-1),1)); if(j==A[i]) G[(i+1)*n+j].push_back(Edge((i*n+j+1),0)); else if(j-1==A[i]) G[(i+1)*n+j].push_back(Edge((i*n+j-1),0)); else G[(i+1)*n+j].push_back(Edge((i*n+j),0)); } } vector dist(n*(m+1),INF); int s=n*m; dist[s]=0; priority_queue,greater> que; que.push({dist[s],s}); while(!que.empty()){ int v=que.top().second; ll d=que.top().first; que.pop(); if(d>dist[v]) continue; for(auto e:G[v]){ if(chmin(dist[e.to],dist[v]+e.w)){ que.push({dist[e.to],e.to}); } } } for(int i=1;i1) cout<<" "; cout<