I presume you start from Budapest, head up north and end-up again in Budapest or Prague.
You may travel by air or rent a car, of course
Here are the basic info, if surface travel on steel rails is what you are into:
For train connections Budapest-Bratislava-Prague, visit
https://www.cd.cz/en/default.htm (travel time 6h30, several services daily)
Then for Praque-Krakow:
https://www.leoexpress.com/en (one service daily dep 16.09, arr 22.57)
Within Poland and for Warsaw-Praque or Warsaw-Budapest, pls check:
https://www.intercity.pl/en/
Estimated travel duration:
Krakow-Warsaw : 2h30
Krakow-Wroclaw- 3h10
Wroclaw-Warsaw - 3h30
Warsaw - Praque: 2 services daytime, 1 servive overnight , 8h daytaime, 12h overnight
Warsaw-Biudapest 1 service daytine 10h30, 1 overnight service 13h
Side note: my theory is that one cannot grab a feel of any city if you don't sleep there AT LEAST 2 nights