Language: C++

#include<bits/stdc++.h>

#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define pb2 pop_back
#define pf2 pop_front
#define rep(k,i,j) for(int k = (int)i;k<(int)j;k++)
#define repd(k,i,j) for(int k = (int)i;k>=(int)j;k--)
#define ll long long
#define line puts("")


const int INF = 1e9+7;
const ll INFLL = 1e17;
const double EPS = 1e-6;

using namespace std;


string A,B;

bool nol(string a){
	rep(k,0,a.size())if(a[k]!='0')return 0;
	return 1;
}

int n;

const int maxn = 2005;
int arr[maxn];

int L[maxn],R[maxn];


int dist(int a,int b){
	if(a>b)swap(a,b);
	
	return min(b-a, a - b + n);
}
int solve(vector<pii> num,int st){
	if(num.size()==0)return st;
	sort(num.begin(),num.end());
	
	int maxr = -INF;
	
	int ret = INF;
	repd(k,num.size()-1,0){
		int ki = num[k].fi;
		
		int ka = maxr + ki;
		if(ka<0)ka = 0;
		
		int sls = ka - ki;
		if(sls < 0)sls += n;
		
		ret = min(ret,ki + ka + dist(sls , st));
		maxr = max(maxr,num[k].se);
	}
	
	return ret;
}
int main(){
	cin>>A>>B;
	
	if(nol(B)){
		printf("%d\n",nol(A)?0:-1);
		return 0;
	}
	
	n = A.size();
	rep(k,0,n)A[k] -= '0',B[k] -='0';
	
	rep(k,0,n){
		if(k)L[k] = L[k-1] + 1;
		if(B[k])L[k] = 0;
	}
	if(!B[0])L[0] = L[n-1]+1;
	
	repd(k,n-1,0){
		if(k!=n-1)R[k] = R[k+1] + 1;
		if(B[k])R[k] = 0;
	}
	if(!B[n-1])R[n-1] = R[0] + 1;
	
	int ret = INF;
	rep(st,0,n){
		rep(k,0,n)arr[(st+k)%n] = A[(st+k)%n]^B[k];
		
		vector<pii> num;
		rep(k,0,n)if(arr[k])num.pb(mp(L[k],R[k]));
		
		//rep(k,0,n)printf("%d ",arr[k]);line;
		//rep(k,0,num.size())printf("(%d %d) ",num[k].fi,num[k].se);line;
		ret = min(ret,solve(num,st) + (int)num.size());
		
		rep(k,0,num.size())swap(num[k].fi,num[k].se);
		
		ret = min(ret,solve(num,st) + (int)num.size());
	}
	cout<<ret<<endl;
	
}#include<bits/stdc++.h>

#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define pb2 pop_back
#define pf2 pop_front
#define rep(k,i,j) for(int k = (int)i;k<(int)j;k++)
#define repd(k,i,j) for(int k = (int)i;k>=(int)j;k--)
#define ll long long
#define line puts("")


const int INF = 1e9+7;
const ll INFLL = 1e17;
const double EPS = 1e-6;

using namespace std;


string A,B;

bool nol(string a){
	rep(k,0,a.size())if(a[k]!='0')return 0;
	return 1;
}

int n;

const int maxn = 2005;
int arr[maxn];

int L[maxn],R[maxn];


int dist(int a,int b){
	if(a>b)swap(a,b);
	
	return min(b-a, a - b + n);
}
int solve(vector<pii> num,int st){
	if(num.size()==0)return st;
	sort(num.begin(),num.end());
	
	int maxr = -INF;
	
	int ret = INF;
	repd(k,num.size()-1,0){
		int ki = num[k].fi;
		
		int ka = maxr + ki;
		if(ka<0)ka = 0;
		
		int sls = ka - ki;
		if(sls < 0)sls += n;
		
		ret = min(ret,ki + ka + dist(sls , st));
		maxr = max(maxr,num[k].se);
	}
	
	return ret;
}
int main(){
	cin>>A>>B;
	
	if(nol(B)){
		printf("%d\n",nol(A)?0:-1);
		return 0;
	}
	
	n = A.size();
	rep(k,0,n)A[k] -= '0',B[k] -='0';
	
	rep(k,0,n){
		if(k)L[k] = L[k-1] + 1;
		if(B[k])L[k] = 0;
	}
	if(!B[0])L[0] = L[n-1]+1;
	
	repd(k,n-1,0){
		if(k!=n-1)R[k] = R[k+1] + 1;
		if(B[k])R[k] = 0;
	}
	if(!B[n-1])R[n-1] = R[0] + 1;
	
	int ret = INF;
	rep(st,0,n){
		rep(k,0,n)arr[(st+k)%n] = A[(st+k)%n]^B[k];
		
		vector<pii> num;
		rep(k,0,n)if(arr[k])num.pb(mp(L[k],R[k]));
		
		//rep(k,0,n)printf("%d ",arr[k]);line;
		//rep(k,0,num.size())printf("(%d %d) ",num[k].fi,num[k].se);line;
		ret = min(ret,solve(num,st) + (int)num.size());
		
		rep(k,0,num.size())swap(num[k].fi,num[k].se);
		
		ret = min(ret,solve(num,st) + (int)num.size());
	}
	cout<<ret<<endl;
	
}

Compiler output:
a.cc:106:2: error: stray '#' in program
  106 | }#include<bits/stdc++.h>
      |  ^
a.cc:106:3: error: 'include' does not name a type
  106 | }#include<bits/stdc++.h>
      |   ^~~~~~~
a.cc:123:10: error: redefinition of 'const long long int INFLL'
  123 | const ll INFLL = 1e17;
      |          ^~~~~
a.cc:18:10: note: 'const long long int INFLL' previously defined here
   18 | const ll INFLL = 1e17;
      |          ^~~~~
a.cc:124:14: error: redefinition of 'const double EPS'
  124 | const double EPS = 1e-6;
      |              ^~~
a.cc:19:14: note: 'const double EPS' previously defined here
   19 | const double EPS = 1e-6;
      |              ^~~
a.cc:129:8: error: redefinition of 'std::string A'
  129 | string A,B;
      |        ^
a.cc:24:8: note: 'std::string A' previously declared here
   24 | string A,B;
      |        ^
a.cc:129:10: error: redefinition of 'std::string B'
  129 | string A,B;
      |          ^
a.cc:24:10: note: 'std::string B' previously declared here
   24 | string A,B;
      |          ^
a.cc:131:6: error: redefinition of 'bool nol(std::string)'
  131 | bool nol(string a){
      |      ^~~
a.cc:26:6: note: 'bool nol(std::string)' previously defined here
   26 | bool nol(string a){
      |      ^~~
a.cc:136:5: error: redefinition of 'int n'
  136 | int n;
      |     ^
a.cc:31:5: note: 'int n' previously declared here
   31 | int n;
      |     ^
a.cc:138:11: error: redefinition of 'const int maxn'
  138 | const int maxn = 2005;
      |           ^~~~
a.cc:33:11: note: 'const int maxn' previously defined here
   33 | const int maxn = 2005;
      |           ^~~~
a.cc:139:5: error: redefinition of 'int arr [2005]'
  139 | int arr[maxn];
      |     ^~~
a.cc:34:5: note: 'int arr [2005]' previously declared here
   34 | int arr[maxn];
      |     ^~~
a.cc:141:5: error: redefinition of 'int L [2005]'
  141 | int L[maxn],R[maxn];
      |     ^
a.cc:36:5: note: 'int L [2005]' previously declared here
   36 | int L[maxn],R[maxn];
      |     ^
a.cc:141:13: error: redefinition of 'int R [2005]'
  141 | int L[maxn],R[maxn];
      |             ^
a.cc:36:13: note: 'int R [2005]' previously declared here
   36 | int L[maxn],R[maxn];
      |             ^
a.cc:144:5: error: redefinition of 'int dist(int, int)'
  144 | int dist(int a,int b){
      |     ^~~~
a.cc:39:5: note: 'int dist(int, int)' previously defined here
   39 | int dist(int a,int b){
      |     ^~~~
a.cc:149:5: error: redefinition of 'int solve(std::vector<std::pair<int, int> >, int)'
  149 | int solve(vector<pii> num,int st){
      |     ^~~~~
a.cc:44:5: note: 'int solve(std::vector<std::pair<int, int> >, int)' previously defined here
   44 | int solve(vector<pii> num,int st){
      |     ^~~~~
a.cc:171:5: error: redefinition of 'int main()'
  171 | int main(){
      |     ^~~~
a.cc:66:5: note: 'int main()' previously defined here
   66 | int main(){
      |     ^~~~