1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N = 1200 ;
4 const int M = 600 ;
5 int p[] = {
6 2,-2,-1,1,-1,1,
7 4,-4,-2,2,-2,2,
8 6,-6,-3,3,-3,3
9 };
10 int q[] = {
11 -1,1,2,-2,-1,1,
12 -2,2,4,-4,-2,2,
13 -3,3,6,-6,-3,3
14 };
15 typedef struct Node{
16 int x,y, step;
17 }Node;
18 int n,a,b,c;
19 int vis[N+5][N+5];
20 void BFS( ){
21 queue<Node>Q;
22 Q.push( Node{0,0,0} );
23
24 vis[M][M] = 1 ;
25 while( !Q.empty() ){
26 Node cur = Q.front();
27 Q.pop();
28 int A = cur.x ;
29 int B = cur.y ;
30 int C = 0 - A - B ;
31 if( n < cur.step ) continue ;
32 if( A == a && B == b && C == c ){
33 cout << cur.step << endl;
34 return ;
35 }
36 for( int i = 0 ; i < 18 ; i++ ){
37 int tx = A + p[i] ;
38 int ty = B + q[i] ;
39 if( -600 <= tx && tx <= 600 && -600 <= ty && ty <= 600 ){
40 if( !vis[tx+M][ty+M] ){
41 Q.push( Node{tx , ty , cur.step+1 } );
42 vis[tx+M][ty+M] = 1 ;
43 }
44 }
45 }
46 }
47 cout << -1 << endl;
48 return ;
49 }
50 int main()
51 {
52 ios_base :: sync_with_stdio(false);
53 cin.tie(NULL) , cout.tie(NULL);
54 cin >> n >> a >> b >> c ;
55 if( a == 0 && b == 0 && c == 0 ){
56 if( n >= 2 ) cout << 2 << endl;
57 else cout << -1 << endl ;
58 }else{
59 BFS();
60 }
61 return 0;
62 }