How long you're at NPS depends on the curriculum (masters program) that you end up doing. Most programs are for 18-24 months I believe. I don't want to get into curriculum choices here though. If you have any questions at all about that kind of stuff, be sure to contact NPS directly. They have the answers. I will say this, however. It pays to take calc based physics and at least two semester of calc, now, before you get commissioned. This is why I always tell people to take these courses REGARDLESS of whether they need them for their major or their commissioning source. If you don't take these classes now, it could potentialy close doors down the road that you might want left open.
As for a flag LT tour, you can't do both that and NPS either back to back or at the same time. You could do a flag LT tour, then your DH tours, and then potentially go to NPS for your masters. But then, you start talking about issues with career timing and whether you're competitive enough to screen for XO, blah, blah, blah. As for getting your masters at a civilian campus while you're a flag LT, that is next to impossible. Flag LT's travel a lot with their respective admirals, making it kind of difficult to attend class. Plus, the hours are long making it double hard. Sure there may be some exceptions, but I certainly wouldn't plan on it. Also, to get a flag LT job, you'll have to have your shi!t squared away. It's a competive screening process that occurs before you assigned that billet. Everyone who wants to do that particular flag LT job tells their detailer their wishes. Then everyone who did has their record screened by BUPERS. The final cut (4 or 5 officers) from the initial record screen are then sent to do a personal interview with not only the admiral who he'd be working for, but also several others on the admiral's staff. My point, being a flag LT is not automatic. If this is something you want, make sure your COC knows this so they can groom you and also so they can put down Flag LT as a recommendation on your fitrep.
If I was in your shoes and able to do it all over again, I would definitely be gunning for a Flag LT tour. I put in for OLA (Office of Legislative Affairs) a couple years back and even got to interview for the job in D.C. There were four of us who interviewed....and none of the four of us got it. I would have had to sacrifice getting my masters degree if I got it though. So, if you already know in advance that this is something you want to pursue, start working on your masters during your divo tours, one class at a time until your are fully qual'd (OOD, SWO, and EOOW). Once you have that golden triangle, start taking two classes at a time. There are PLENTY of schools that offer masters programs via distance education. The navy could care less where you get your degree from or what it's in. Then, if you get your flag LT job, great, you're set. If not, then pick something that really interests you and enjoy your shore duty.