#include using namespace std; using ll = long long; int main (){ int n,d; cin>>n>>d; if(n==1){ cout << "0\n"; return 0; } vector> pts; for(int i=0; i curr(d); for(int j=0; j>curr[j]; pts.push_back(curr); } sort(pts.begin(), pts.end()); for(int j=0; j pts[1][j]){ for(int i=0;i dists; vector> by_coord(d); for(int i=1; i pts[i][j]){ cout << "-1\n"; return 0; } dist += pts[i][j] - pts[i-1][j]; } dists.push_back(dist); } for(int i=0; i to_equal(d, 0); for(int j=0; j=0; i--){ tg -= dists[i]; val += abs(tg - pts[i][j]); } res=min(res, val); } cout<