Atomic beam measurement of the lifetime of 3P1 metastable states of Mg and Ca