You have two moves, and you must pass the distance test and the time test.
From the same publication 521:
Distance test:
Your move will meet the distance test if your new main job location is
at least 50 miles farther from your former home than your old main job
location was from your former home. For example, if your old main job
location was 3 miles from your former home, your new main job location
must be at least 53 miles from that former home. You can use Worksheet
1 to see if you meet this test.
Time Test:
Time Test for Employees
If you are an employee, you must work full time for at least 39 weeks
during the first 12 months after you arrive in the general area of
your new job location (39-week test). Full-time employment depends on
what is usual for your type of work in your area.
For purposes of this test, the following four rules apply.
You count only your full-time work as an employee, not any work you do as a self-employed person.
You do not have to work for the same employer for all 39 weeks.
You do not have to work 39 weeks in a row.
You must work full time within the same general commuting area for all 39 weeks.
Let us assume:
- you live in City A and work 5 miles away.
- You thought you had a job 60 miles from City A so you moved to city B.
- It fell apart after a month
- you have another job
- you moved back to City A.
The law doesn't care about the distance between the new home and the new job, only the distance between the new job and the old home compared to the old home and the old job.
Move #1: you fail the time test
Move #2: you might eventually pass the time test if you work 39 of 52 weeks, but what about the distance test. If you had a new job that was close to city B you could eventually claim Move #1 if you hadn't moved.
Combining both:
If Job #3 was more than 50 miles farther from house #1 when compared to job #1 and house #1; you might have a case to claim some expenses, assuming you eventually worked 39 of 52 weeks. The problem is if the distance isn't far enough you run the risk of the IRS rejecting the entire thing.
They would be concerned that somebody could spend a few bucks on a short duration cheap move to city B and then claim all the expenses for a move that was essentially across the street. You would have a hard time proving that the move to City B was intended to be permanent: you never established residency.