//Sam Powers

//ENLOE HS

//ACSL 4 INT-5

 

#include <iostream.h>

#include <conio.h>

 

class LL {

            private:

                        struct Node {

                                    int hori, vert;

                                    Node *next;

                                    Node (int x, int y, Node *n){

                                                hori = x;

                                                vert = y;

                                     //         cout<<x;

                                     //         cin.get();

                                                next = n;

                                    }

                        };

            public:

                        Node *first;

                        void Create();

                        int Check(int h, int v);

                        void Cfir(int h, int v);

                        void Add(int h, int v);

};

//should probably find a duplicate-finder, and if there are duplicate nodes

//remove one

void LL::Add(int h, int v){

            Node *current;

            current = first;

            while (current->next){

                        current=current->next;

            }

            current->next = new Node(h, v, NULL);

}

void LL::Create(){

            const n = 1000;

            char tem[n];

            int tra=4;

            Node *current;

 //         cout<<"Please create the node set now.";

            cin>>tem;//play with getline when done

            first = new Node(tem[0]-48,tem[2]-48,NULL);

            current = first;

            while((tem[tra]!=0)&&(tem[tra+2]!=0)){

                        current->next = new Node(tem[tra]-48, tem[tra+2]-48, NULL);

                        current = current->next;

                        tra = tra+4;

            }

 

}

int LL::Check(int h, int v){

            Node *current;

            current = first;

            while((current)&&(current->hori!=h)&&(current->vert!=v)){

                        current = current->next;

            }

            if (!current) return(0);

                        else return(1);

}

void LL::Cfir(int h, int v) {first = new Node(h, v, NULL);}

 

 

int PFind(int h1, int v1, int h2, int v2, LL Ptem, LL Path){

//h1,v1 is where finder is atm, h2, v2 is where trying to go

//this function sees if there is a path around the node, and if there is

//goes there, if h2, v2 found, finishes

            int temp=0;

            if ((h1 == h2)&&(v1 == v2)) return(1);

            else if ((Path.Check(h1+1,v1) == 1)&&(Ptem.Check(h1+1,v1) == 0)){

                        Ptem.Add(h1+1,v1);

                        temp = temp + PFind(h1+1,v1, h2, v2, Ptem, Path);

            }

                        else if ((Path.Check(h1,v1+1) == 1)&&(Ptem.Check(h1,v1+1) == 0)){

                                    Ptem.Add(h1,v1+1);

                                    temp = temp + PFind(h1,v1+1, h2, v2, Ptem, Path);

                        }

                                    else if ((Path.Check(h1-1,v1) == 1)&&(Ptem.Check(h1-1,v1) == 0)){

                                                Ptem.Add(h1-1,v1);

                                                temp = temp + PFind(h1-1,v1, h2, v2, Ptem, Path);

                                    }

                                                else if ((Path.Check(h1,v1-1) == 1)&&(Ptem.Check(h1,v1-1) == 0)){

                                                            Ptem.Add(h1,v1-1);

                                                            temp = temp + PFind(h1,v1-1, h2, v2, Ptem, Path);

                                                }

}

 

void main() {

            LL Path, Ptem;

            Path.Create();

            int h1,v1,h2,v2, temp;

            char tem[7];

  //        cout<<"Please enter node pairs.";

  for (int x = 0; x<5; x++){

            cin>>tem;

            h1 = int(tem[0])-48;

            v1 = int(tem[3])-48;

            h2 = int(tem[5])-48;

            v2 = int(tem[5])-48;

            Ptem.Cfir(h1,v1);

            temp = temp + PFind(h1,v1,h2,v2, Ptem, Path);

            if (temp != 0) cout<<temp;

                        else cout<<"NONE";

            getch();

  }

}