One ad showed Tonias this grid. Like many people, Tonias is frequently plagued by mobile game ads. He is just watching some videos, ignoring the ads, when he notices something strange in one of them.
The game is quite simple: given some arrows in a rectangular grid, the goal is to remove all arrows. An arrow may be removed if it is not pointing at any arrow, including itself (i.e. no part of any arrow is straight in front of the arrow head at any distance). However, Tonias is fairly sure that it is not possible to remove all arrows!
This interruption has already taken too much time. He is now behind on his anime episodes, which means he is behind on going to bed, and thus on getting up and working on his thesis. It is, frankly, rather irritating. Write a program to help him determine how many arrows can actually be removed.
Visualization of Sample Input 1. Arrows occur in the order $$$ABCD$$$ in the input. Since $$$A$$$ points at both $$$B$$$ and $$$C$$$, $$$B$$$ points at $$$C$$$, and $$$C$$$ points at $$$D$$$, the only removal order is $$$DCBA$$$. The input consists of:
Output the number of arrows that can be removed.
5 4 4 2 2 LLDDR 1 1 RDR 4 0 ULU 4 2 ULLLL
4
3 3 2 0 2 DDR 2 0 UUL
0