#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
using namespace std;
typedef long long LL;
typedef long double LD;
typedef complex<double> CMP;
class TurnOnLamps { public:
typedef int Vert;
struct Edge {
bool init;
bool goal;
Vert from;
Vert to;
};
int minimize(vector <int> roads, string initState, string isImportant)
{
int N = roads.size() + 1;
vector< vector<Edge> > G(N);
for(int i=0; i<roads.size(); ++i)
{
int v = i+1;
int u = roads[i];
{Edge e = {initState[i]=='1', isImportant[i]=='1', v, u}; G[v].push_back(e);}
{Edge e = {initState[i]=='1', isImportant[i]=='1', u, v}; G[u].push_back(e);}
}
Edge e = {false, false, -1, 0};
pair<int,int> rr = rec(e, G);
return min(rr.first, rr.second);
}
static const int INF = 999999;
// not use, use
pair<int,int> rec(Edge e, vector<vector<Edge> >& G)
{
vector< pair<int,int> > p;
for(int i=0; i<G[e.to].size(); ++i)
{
Edge& e2 = G[e.to][i];
if(e2.to != e.from)
p.push_back(rec(e2, G));
}
pair<int,int> r = calc_on_node(p);
if(e.goal)
(e.init ? r.second : r.first) = INF;
return r;
}
pair<int,int> calc_on_node(vector<pair<int,int> >& c)
{
int nu = calc(c);
int u = (c.empty() ? 1 : INF);
for(int i=0; i<c.size(); ++i)
{
vector<pair<int,int> > cc = c;
cc.erase(cc.begin() + i);
int s1 = calc(cc);
int s2 = min(c[i].first + 1, c[i].second);
u = min(u, s1+s2);
}
return make_pair(nu, u);
}
int calc(vector<pair<int,int> >& c)
{
int od = INF;
int ev = 0;
for(int i=0; i<c.size(); ++i)
{
int c0 = c[i].first;
int c1 = c[i].second;
int od2 = min(ev+c1, od+c0);
int ev2 = min(ev+c0, od+c1-1);
od = od2, ev = ev2;
}
return min(od, ev);
}
};
// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
{ ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
{ os << "{ ";
for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
bool ok = (Expected == Received);
if(ok) cerr << "PASSED" << timer() << endl; else { cerr << "FAILED" << timer() << endl;
cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END verify_case(_, TurnOnLamps().minimize(roads, initState, isImportant));}
int main(){
CASE(0)
int roads_[] = {0,0,1,1};
vector <int> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_));
string initState = "0001";
string isImportant = "0111";
int _ = 1;
END
CASE(1)
int roads_[] = {0,0,1,1};
vector <int> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_));
string initState = "0000";
string isImportant = "0111";
int _ = 2;
END
CASE(2)
int roads_[] = {0,0,1,1,4,4};
vector <int> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_));
string initState = "000100";
string isImportant = "111111";
int _ = 2;
END
CASE(3)
int roads_[] = {0,0,1,1,4,4};
vector <int> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_));
string initState = "100100";
string isImportant = "011101";
int _ = 2;
END
CASE(4)
int roads_[] = {0,0,2,2,3,1,6,3,1};
vector <int> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_));
string initState = "010001110";
string isImportant = "000110100";
int _ = 1;
END
CASE(5)
int roads_[] = {0,0,1,2,4,4,6,1,2,5,2,8,8,3,6,4,14,7,18,14,11,7,1,12,7,5,18,23,0,14,11,10,2,2,6,1,30,11,9,12,5,35,25,11,23,17,14,45,15};
vector <int> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_));
string initState = "0000000000010000000000000010000010100000000000000";
string isImportant = "1010111111111011011111000110111111111111111110111";
int _ = 14;
END
/*
CASE(6)
int roads_[] = ;
vector <int> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_));
string initState = ;
string isImportant = ;
int _ = ;
END
CASE(7)
int roads_[] = ;
vector <int> roads(roads_, roads_+sizeof(roads_)/sizeof(*roads_));
string initState = ;
string isImportant = ;
int _ = ;
END
*/
}
// END CUT HERE