The strategy here is to count:
Number of ways = Total number of ways that PMSJ can stand together - Number of ways that SJ stand next to eachother
The total number of ways that PMSJ can stand next to each other is equal to 4! = 4321 = 24.
The number of ways that SJ will end up standing next to eachother is 6: (SJPM, PSJM, PMSJ, JSPM, PJSM, PMJS)
So, 24 - 6 = 18 ways.