A. Pavel and barbecue

Codeforces
IDCF756A
Time2000ms
Memory256MB
Difficulty
constructive algorithmsdfs and similar
Pavel cooks barbecue. There are _n_ skewers, they lay on a brazier in a row, each on one of _n_ positions. Pavel wants each skewer to be cooked some time in every of _n_ positions in two directions: in the one it was directed originally and in the reversed direction. Pavel has a plan: a permutation _p_ and a sequence _b_1, _b_2, ..., _b__n_, consisting of zeros and ones. Each second Pavel move skewer on position _i_ to position _p__i_, and if _b__i_ equals 1 then he reverses it. So he hope that every skewer will visit every position in both directions. Unfortunately, not every pair of permutation _p_ and sequence _b_ suits Pavel. What is the minimum total number of elements in the given permutation _p_ and the given sequence _b_ he needs to change so that every skewer will visit each of 2_n_ placements? Note that after changing the permutation should remain a permutation as well. There is no problem for Pavel, if some skewer visits some of the placements several times before he ends to cook. In other words, a permutation _p_ and a sequence _b_ suit him if there is an integer _k_ (_k_ ≥ 2_n_), so that after _k_ seconds each skewer visits each of the 2_n_ placements. It can be shown that some suitable pair of permutation _p_ and sequence _b_ exists for any _n_. ## Input The first line contain the integer _n_ (1 ≤ _n_ ≤ 2·105) — the number of skewers. The second line contains a sequence of integers _p_1, _p_2, ..., _p__n_ (1 ≤ _p__i_ ≤ _n_) — the permutation, according to which Pavel wants to move the skewers. The third line contains a sequence _b_1, _b_2, ..., _b__n_ consisting of zeros and ones, according to which Pavel wants to reverse the skewers. ## Output Print single integer — the minimum total number of elements in the given permutation _p_ and the given sequence _b_ he needs to change so that every skewer will visit each of 2_n_ placements. [samples] ## Note In the first example Pavel can change the permutation to 4, 3, 1, 2. In the second example Pavel can change any element of _b_ to 1.
Samples
Input #1
4
4 3 2 1
0 1 1 1
Output #1
2
Input #2
3
2 3 1
0 0 0
Output #2
1
API Response (JSON)
{
  "problem": {
    "name": "A. Pavel and barbecue",
    "description": {
      "content": "Pavel cooks barbecue. There are _n_ skewers, they lay on a brazier in a row, each on one of _n_ positions. Pavel wants each skewer to be cooked some time in every of _n_ positions in two directions: i",
      "description_type": "Markdown"
    },
    "platform": "Codeforces",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "CF756A"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Pavel cooks barbecue. There are _n_ skewers, they lay on a brazier in a row, each on one of _n_ positions. Pavel wants each skewer to be cooked some time in every of _n_ positions in two directions: i...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments