In this case we have the following equation:

What we must do is replace the value of M for each of the cases.
We have then:
For San Francisco:
For Bam, Iran: 
The difference between both maginitudes is:
Answer:
The energy of the San Francisco earthquake was 2030000 J larger than the earthquake energy of the city of Bam, Iran.