cho.sh
Notes
Loading...

Rockstar Rock Dongho

Time limit

2s

Memory limit

128 MB

Problem

Rock Dongho is choosing songs for a new album. Each song has a starting tempo and an ending tempo, and each tempo is either fast (F) or slow (S). There are FF songs that start fast and end fast, FS songs that start fast and end slow, SF songs that start slow and end fast, and SS songs that start slow and end slow.

Each selected song may appear on the album at most once. The order of songs must satisfy the following conditions.

  1. For every song except the first one, its starting tempo must be the same as the ending tempo of the song immediately before it.
  2. If at least one recorded song starts fast, the first song on the album must start fast. If no recorded song starts fast, this condition does not apply.

Find the maximum number of songs Dongho can put on the album while satisfying all conditions.

Input

The first line contains four integers FF FS SF SS in that order. Each integer is between 0 and 1,000 inclusive, and at least one of them is positive.

Output

Print the maximum number of songs Dongho can put on the album while satisfying the conditions.