A=$<.map{|s|s.split.map &:to_i} m=(1..M=2e3).map{[0]*M} A.pop(A.shift[1]).map{|x,y,w,h,d|(x..x+w).map{|i|m[i][y]+=d;m[i][y+h+1]-=d}} (R=-500..1e3).map{|x|R.map{|y|m[x][y+1]+=m[x][y]}} p A.map{|x,y,h|[0,h-m[x][y]].max}.sum