import Data.List

m s [] [] = (s,[])
m s (a:as) (b:bs) =
  let (s',r) = m s as bs
      s'' = if a == 1 && b == 1 then 1 else 0
      r'' = if a == 1
            then if b == 1
                 then 0
                 else 1
            else 0
  in
    (s'+s'',r'':r)

o [] [] = []
o (a:as) (b:bs) =
  let r = if a==1 || b==1 then 1 else 0
  in
    r:(o as bs)

ans' _ [] _ = 0
ans' s (a:as) bs =
  let s' = o s a
      r = (map (\b -> m 0 s' b) bs)
      (n,x) = maximumBy (\ (a,al) (b,bl) -> compare a b) r
  in
    n + (ans' x as bs)

ans ([0,0]:_) = []
ans ([n,c]:x) =
  let a = take n x
      b = take c $ drop n x
      z = take 16 $ repeat 0 
      r = drop (n+c) x
  in
    (ans' z a b):(ans r)

main = do
  c <- getContents
  let i = map (map read) $ map words $ lines c :: [[Int]]
      o = ans i
  mapM_ print o

