Federer overcomes Nagal scare to reach second round