Assuming your data is in columns A:E, then in F2 try:
=(LOOKUP(2,1/($A$2:$A$25=A2),$B$2:$B$25+$C$2:$C$25)-INDEX($D$2:$D$25+$E$2:$E$25,MATCH(A2,$A$2:$A$25,0)))*1440
adjust the ranges to cover your whole database (don't use too many more rows than necessary), then copy down.