#include using namespace std; int main(){ ios_base::sync_with_stdio(false); cin.tie(nullptr); int H,W; cin >> H >> W; if(H*W == 1){cout << 1 << endl; return 0;} int N = H*W; vector> G(N); vector V(N+1); for(int i=1; i<=N; i++) if(N%i == 0) for(int k=i; k<=N; k+=i) V.at(k) = i; V.at(N) = 1; for(int i=1; i<=N; i++) G.at(V.at(i)).push_back(i); for(int i=2; i W) swap(H,W),S = true; vector> A(H,vector(W)),B = A; int x = 0,y = 0,L = 0,ok = 0; bool dx = false; vector P(N); auto get = [&]() -> pair { if(dx) return {x,L}; else return {L,y}; }; auto put = [&](int v) -> void { assert(G.at(v).size()); ok++; int now = G.at(v).back(); G.at(v).pop_back(); auto [X,Y] = get(); A.at(X).at(Y) = now,B.at(X).at(Y) = v; if(dx) x++; else y++; if(ok == N) return; while(true){ tie(X,Y) = get(); if(!dx){ if(X >= H || Y >= W || X < Y) dx = true,x = 0,y = 0,L++; else break; } else{ if(X >= H || Y >= W || X == Y) dx = false; else break; } } }; for(int i=1; i= N) lcm = 1; while(lcm >= 2 && P.at(lcm) < N && G.at(P.at(lcm)).size() == 0) P.at(lcm) += lcm; if(P.at(lcm) >= N) lcm = 1; //cout << X << " " << Y << " " << P.at(lcm) << endl; assert(G.at(P.at(lcm)).size()); put(P.at(lcm)); } if(S) swap(H,W); for(int i=0; i