I don't understand your flow - do you mean that code is on both the first and last pages? Where on the last page are you setting the variable to "2", and how are you getting from the last page to the first page (or, for that matter, from the first page to the last page)?
Could you be a bit more explicit about the process? Basically, something that someone else can run that shows just what you mean?
(As an aside, you don't need to see if a variable is set before unsetting it.)