You simple follow the signs, follow the signs to the other terminal, go to departures and follow the signs again. Passport and customs will be in Munich. Your flight to Madrid will be treated as a domestic flight. Munich cannot handle 30 minutes connections of your kind but three hours are really comfortable.