#include<bits/stdc++.h>
using namespace std;

int main() {
    string s;
    cin >> s;
    int m;
    cin >> m;
    int a[s.length()] = {0};
    if(s[0] == s[1])
        a[0] = 1;
    for(int i = 1; i < s.length() - 1; i++) {
        if(s[i] == s[i+1])
            a[i] = a[i-1] + 1;
    }
    a[s.length()-1] = a[s.length()-2] + 1;
    while(m--) {
        int l, r;
        cin >> l >> r;
        cout << a[r-1] - a[l-1] << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main() {
    string s;
    cin >> s;
    int m;
    cin >> m;
    int a[s.length()] = {0};
    if(s[0] == s[1])
        a[0] = 1;
    for(int i = 1; i < s.length() - 1; i++) {
        if(s[i] == s[i+1])
            a[i] = a[i-1] + 1;
    }
    while(m--) {
        int l, r;
        cin >> l >> r;
        cout << a[r-1] - a[l-1] << endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main() {
    string s;
    cin >> s;
    int m;
    cin >> m;
    while(m--) {
        int l, r;
        cin >> l >> r;
        l--;
        r--;
        int count = 0;
        for(int i = l; i < r; i++)
            if(s[i] == s[i+1])
                count++;
        cout << count << endl;
    }
}


#include <iostream>
#include<cstring>
using namespace std;
 int main()

 {
       ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string s;
   cin>>s;

long long int NoOfQrs;
   cin>>NoOfQrs;
   //took input of number of queries.

   long long int arrL[NoOfQrs];//array for first index of each query.
   long long int arrR[NoOfQrs];//array for second index of each query.

long long int i=0;
for(i=0;i<NoOfQrs;i++)
{
    cin>>arrL[i]>>arrR[i];
}//took input of queries.

long long int answer=0;
for(i=0;i<NoOfQrs;i++)

{
    for(long long int k=arrL[i];k<arrR[i];k++){
    if(s[k-1]==s[k])
    {
        answer++;
    }
    else continue;
    }
    cout<<answer<<endl;  //Printing the answer part.
    answer=0;
}
    return 0;
 }


#include <bits/stdc++.h>

using namespace std;
 int main()

 {
       ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string s;
   cin>>s;

 int m;
   cin>>m;

   int l[m];
      int r[m];
      for(int i=0;i<m;++i)
      {
          cin>>l[i]>>r[i];
      }
      int k=s.length()-1;
      int arvin[k]={0};
      for(int i=0;i<k;i++)
      {
          if(s[i]==s[i+1])
          {
              arvin[i]=1;
          }
          else{arvin[i]=0;}
      }
      int psa[k]={0};
      psa[0]=arvin[0];
      for(int i=1;i<k;i++)
      {
          psa[i]=psa[i-1]+arvin[i];
      }

     int answer[m]={0};
      for(int i=0;i<m;++i)
      {
          if(l[i]==1)
          {
              answer[i]=psa[r[i]-2]-psa[l[i]-1];
          }
          else{

          answer[i]= psa[r[i]-2]-psa[l[i]-2];}

      }
      for(int i=0;i<m;++i)
      {
          cout<<answer[i]<<endl;
      }

 }


#include <bits/stdc++.h>
#include<cstring>
using namespace std;
 int main()

 {
       ios_base::sync_with_stdio(false);
    cin.tie(NULL);

   string s;
   cin>>s;

int NoOfQrs;
   cin>>NoOfQrs;
   //took input of number of queries.

   int arrL[NoOfQrs];//array for first index of each query.
   int arrR[NoOfQrs];//array for second index of each query.

int i=0;
for(i=0;i<NoOfQrs;i++)
{
    cin>>arrL[i]>>arrR[i];
}//took input of queries.

int answer=0;
for(i=0;i<NoOfQrs;i++)

{
    for(int k=arrL[i];k<arrR[i];k++){
    if(s[k-1]==s[k])
    {
        answer++;
    }
    }
    cout<<answer<<endl;  //Printing the answer part.
    answer=0;
}

 }


#include <bits/stdc++.h>
#include<cstring>
using namespace std;
 int main()

 {
       ios_base::sync_with_stdio(false);
    cin.tie(NULL);

   string s;
   cin>>s;

int NoOfQrs;
   cin>>NoOfQrs;
   //took input of number of queries.

   int arrL[NoOfQrs];//array for first index of each query.
   int arrR[NoOfQrs];//array for second index of each query.

int i=0;
for(i=0;i<NoOfQrs;i++)
{
    cin>>arrL[i]>>arrR[i];
}//took input of queries.

int answer=0;
for(i=0;i<NoOfQrs;i++)

{
    for(int k=arrL[i];k<arrR[i];k++){
    if(s[k-1]==s[k])
    {
        answer++;
    }
    else continue;
    }
    cout<<answer<<endl;  //Printing the answer part.
    answer=0;
}
    return 0;
 }


#include <bits/stdc++.h>
#include<cstring>
using namespace std;
 int main()

 {
       ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string s;
   cin>>s;

long long int NoOfQrs;
   cin>>NoOfQrs;
   //took input of number of queries.

   long long int arrL[NoOfQrs];//array for first index of each query.
   long long int arrR[NoOfQrs];//array for second index of each query.

long long int i=0;
for(i=0;i<NoOfQrs;i++)
{
    cin>>arrL[i]>>arrR[i];
}//took input of queries.

long long int answer=0;
for(i=0;i<NoOfQrs;i++)

{
    for(long long int k=arrL[i];k<arrR[i];k++){
    if(s[k-1]==s[k])
    {
        answer++;
    }
    else continue;
    }
    cout<<answer<<endl;  //Printing the answer part.
    answer=0;
}
    return 0;
 }


#include <bits/stdc++.h>
#include<cstring>
using namespace std;
 int main()

 {

   string s;
   cin>>s;

   int NoOfQrs;
   cin>>NoOfQrs;
   //took input of number of queries.

   int arrL[NoOfQrs];//array for first index of each query.
   int arrR[NoOfQrs];//array for second index of each query.

int i=0;
for(i=0;i<NoOfQrs;i++)
{
    cin>>arrL[i]>>arrR[i];
}//took input of queries.

int answer=0;
for(i=0;i<NoOfQrs;i++)

{
    for(int k=arrL[i];k<arrR[i];k++){
    if(s[k-1]==s[k])
    {
        answer++;
    }
    }
    cout<<answer<<endl;  //Printing the answer part.
    answer=0;
}

 }


#include <bits/stdc++.h>
#include<cstring>
using namespace std;
 int main()

 {

   string s;
   cin>>s;
   int len=0;
   while(s[len] != '\0')
   {
       len++;
       //took input of the string and calculated its length.
   }

   int NoOfQrs;
   cin>>NoOfQrs;
   //took input of number of queries.

   int arrL[NoOfQrs];//array for first index of each query.
   int arrR[NoOfQrs];//array for second index of each query.

int i=0;
for(i=0;i<NoOfQrs;i++)
{
    cin>>arrL[i]>>arrR[i];
}//took input of queries.

int strTOint[len];
for(int j=0;j<len;j++)
{
  strTOint[j] = s[j] ;
}

int answer=0;
for(i=0;i<NoOfQrs;i++)

{
    for(int k=arrL[i];k<arrR[i];k++){
    if(strTOint[k-1]==strTOint[k])
    {
        answer++;
    }
    }
    cout<<answer<<endl;  //Printing the answer part.
    answer=0;
}

 }


#include <bits/stdc++.h>

using namespace std;
 int main()

 {
       ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    string s;
   cin>>s;

 int m;
   cin>>m;

   int l[m];
      int r[m];
      for(int i=0;i<m;++i)
      {
          cin>>l[i]>>r[i];
      }
      int k=s.length()-1;
      int arvin[k]={0};
      for(int i=0;i<k;i++)
      {
          if(s[i]==s[i+1])
          {
              arvin[i]=1;
          }
          else{arvin[i]=0;}
      }
      int psa[k]={0};
      psa[0]=arvin[0];
      for(int i=1;i<k;i++)
      {
          psa[i]=psa[i-1]+arvin[i];
      }

     int answer[m]={0};
      for(int i=0;i<m;++i)
      {
          if(l[i]==1)
          {
              answer[i]=psa[r[i]-2];
          }
          else{

          answer[i]= psa[r[i]-2]-psa[l[i]-2];}

      }
      for(int i=0;i<m;++i)
      {
          cout<<answer[i]<<endl;
      }

 }


#include <bits/stdc++.h>
#include<cstring>
using namespace std;
 int main()

 {
       ios_base::sync_with_stdio(false);
    cin.tie(NULL);

   string s;
   cin>>s;

long long int NoOfQrs;
   cin>>NoOfQrs;
   //took input of number of queries.

   long long int arrL[NoOfQrs];//array for first index of each query.
   long long int arrR[NoOfQrs];//array for second index of each query.

int i=0;
for(i=0;i<NoOfQrs;i++)
{
    cin>>arrL[i]>>arrR[i];
}//took input of queries.

int answer=0;
for(i=0;i<NoOfQrs;i++)

{
    for(long long int k=arrL[i];k<arrR[i];k++){
    if(s[k-1]==s[k])
    {
        answer++;
    }
    else continue;
    }
    cout<<answer<<endl;  //Printing the answer part.
    answer=0;
}
    return 0;
 }


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define x first
#define y second
#define Identify(x) cerr << #x << " is " << x << endl;
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (int i = 1; i < n; i++)
#define FORE(i, n) for (int i = 0; i <= n; i++)
#define FORE1(i, n) for (int i = 1; i <= n; i++)

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    string s;
    int m;
    cin >> s >> m;
    int n = s.size();
    vector<vector<int>> v(m, vector<int> (2));
    FOR(i, m){
        for (int j = 0; j <= 1; j++){
            cin >> v[i][j];
            v[i][j]--;
            // set up for the same index with vector
        }
    }
    vector<int> ans(n,0);
    if (s[0] == s[1]) ans[0]++;
    for (int i = 1; i < n - 1; i++){
        ans[i] = ans[i-1];
        if (s[i] == s[i+1])
            ans[i]++;
    }
    for (int i = 0; i < m; i++){
        int temp = ans[v[i][1]-1];
        cout << temp;
        // max at point v[i][1] - 1
        if (v[i][0] >= 1){
            temp -= ans[v[i][0]-1];
        }
        // subtract at point v[i][0] - 1
        cout << temp << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define x first
#define y second
#define Identify(x) cerr << #x << " is " << x << endl;
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (int i = 1; i < n; i++)
#define FORE(i, n) for (int i = 0; i <= n; i++)
#define FORE1(i, n) for (int i = 1; i <= n; i++)

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    string s;
    int m;
    cin >> s >> m;
    int n = s.size();
    vector<vector<int>> v(m, vector<int> (2));
    FOR(i, m){
        for (int j = 0; j <= 1; j++){
            cin >> v[i][j];
            v[i][j]--;
        }
    }
    vector<int> ans(n,0);
    for (int i = 0; i < n - 1; i++){
        if (i != 0) ans[i] = ans[i-1];
        if (s[i] == s[i+1])
            ans[i]++;
    }
    ans[n-1] = ans[n-2];
    for (auto x: ans){
        cout << x << " ";
    }
    cout << endl;
    for (int i = 0; i < m; i++){
        int temp = (v[i][0] == 0) ? ans[v[i][1]-1] : ans[v[i][1]-1] - ans[v[i][0]-1];
        cout << temp << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define x first
#define y second
#define Identify(x) cerr << #x << " is " << x << endl;
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (int i = 1; i < n; i++)
#define FORE(i, n) for (int i = 0; i <= n; i++)
#define FORE1(i, n) for (int i = 1; i <= n; i++)

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    string s;
    int n;
    cin >> s >> n;
    vector<vector<int>> v(n, vector<int> (2));
    FOR(i, n){
        for (int j = 0; j <= 1; j++){
            cin >> v[i][j];
        }
    }
    vector<int> ans;
    for (int i = 0; i < n; i++){
        int cnt = 0;
        for (int j = v[i][0]; j < v[i][1]; j++){
            if (s[j-1] == s[j]){
                cnt++;
            }
        }
        ans.push_back(cnt);
    }
    for (auto x: ans)
        cout << x << endl;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define x first
#define y second
#define Identify(x) cerr << #x << " is " << x << endl;
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (int i = 1; i < n; i++)
#define FORE(i, n) for (int i = 0; i <= n; i++)
#define FORE1(i, n) for (int i = 1; i <= n; i++)

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    string s;
    int m;
    cin >> s >> m;
    int n = s.size();
    vector<vector<int>> v(m, vector<int> (2));
    FOR(i, m){
        for (int j = 0; j <= 1; j++){
            cin >> v[i][j];
            v[i][j]--;
        }
    }
    vector<int> ans(n,0);
    for (int i = 0; i < n - 1; i++){
        if (i != 0) ans[i] = ans[i-1];
        if (s[i] == s[i+1])
            ans[i]++;
    }
    ans[n-1] = ans[n-2];
//    for (auto x: ans){
//        cout << x << " ";
//    }
//    cout << endl;
    for (int i = 0; i < m; i++){
        int temp = (v[i][0] == 0) ? ans[v[i][1]-1] : ans[v[i][1]-1] - ans[v[i][0]-1];
        cout << temp << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define x first
#define y second
#define Identify(x) cerr << #x << " is " << x << endl;
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (int i = 1; i < n; i++)
#define FORE(i, n) for (int i = 0; i <= n; i++)
#define FORE1(i, n) for (int i = 1; i <= n; i++)

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    string s;
    int m;
    cin >> s >> m;
    int n = s.size();
    vector<vector<int>> v(m, vector<int> (2));
    FOR(i, m){
        for (int j = 0; j <= 1; j++){
            cin >> v[i][j];
            v[i][j]--;
            // set up for the same index with vector
        }
    }
    vector<int> ans(n,0);
    if (s[0] == s[1]) ans[0]++;
    for (int i = 1; i < n - 1; i++){
        ans[i] = ans[i-1];
        if (s[i] == s[i+1])
            ans[i]++;
    }
    for (int i = 0; i < m; i++){
        int temp = ans[v[i][1]-1];
        // max at point v[i][1] - 1
        if (v[i][0] >= 1){
            temp -= ans[v[i][0]-1];
        }
        // subtract at point v[i][0] - 1
        cout << temp << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define x first
#define y second
#define Identify(x) cerr << #x << " is " << x << endl;
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (int i = 1; i < n; i++)
#define FORE(i, n) for (int i = 0; i <= n; i++)
#define FORE1(i, n) for (int i = 1; i <= n; i++)

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    string s;
    int m;
    cin >> s >> m;
    int n = s.size();
    vector<vector<int>> v(m, vector<int> (2));
    FOR(i, m){
        for (int j = 0; j <= 1; j++){
            cin >> v[i][j];
            v[i][j]--;
            // set up for the same index with vector
        }
    }
    vector<int> ans(n,0);
    if (s[0] == s[1]) ans[0]++;
    for (int i = 1; i < n - 1; i++){
        ans[i] = ans[i-1];
        if (s[i] == s[i+1])
            ans[i]++;
    }
    for (int i = 0; i < m; i++){
        int temp = ans[v[i][1]-1];
        // max at point v[i][1] - 1
        if (v[i][0] > 1){
            temp -= ans[v[i][0]-1];
        }
        // subtract at point v[i][0] - 1
        cout << temp << endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define x first
#define y second
#define Identify(x) cerr << #x << " is " << x << endl;
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (int i = 1; i < n; i++)
#define FORE(i, n) for (int i = 0; i <= n; i++)
#define FORE1(i, n) for (int i = 1; i <= n; i++)

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    string s;
    int n;
    cin >> s >> n;
    vector<vector<int>> v(n, vector<int> (2));
    FOR(i, n){
        for (int j = 0; j <= 1; j++){
            cin >> v[i][j];
        }
    }
    vector<int> ans;
    for (int i = 0; i < n; i++){
        int cnt = 0;
        for (int j = v[i][0]-1; j < v[i][1]-1; j++){
            if (s[j] == s[j+1]){
                cnt++;
            }
        }
        ans.push_back(cnt);
    }
    for (auto x: ans)
        cout << x << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    cin>>s;
    int n,m,i,l,r,x;
    n=s.length();
    cin>>m;
    while(m--)
    {
        cin>>l>>r;
        l--,r--;
        x=0;
        for(i=l;i<r;i++)
        {
            if(s[i]==s[i+1])
                x++;
        }
        cout<<x<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    cin>>s;
    int n,m,i,l,r,x,e;
    n=s.length();
    cin>>m;
    e=ceil(sqrt(n));
    // cout<<"e:"<<e<<endl;
    int a[e]={0};
    for(i=0;i<n-1;i++)
        if(s[i]==s[i+1])
            a[i/e]++;
    // for(i=0;i<e;i++)
    //     cout<<a[i]<<" ";
    // cout<<endl;
    while(m--)
    {
        cin>>l>>r;
        l--,r-=2;
        x=0;
        for(i=l;i<=r;i++)
        {
            if(i%e==0 && r-i>=e-1)
            {
                x+=a[i/e];
                i+=e-1;
            }
            else if(s[i]==s[i+1])
            {
                x++;
            }
        }
        cout<<x<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    cin>>s;
    int n,m,i,l,r,x,e;
    n=s.length();
    cin>>m;
    e=ceil(sqrt(n));
    int a[e]={0};
    for(i=0;i<n-1;i++)
        if(s[i]==s[i+1])
            a[i/e]++;
    while(m--)
    {
        cin>>l>>r;
        l--,r--;
        x=0;
        for(i=l;i<r;)
        {
            if(i%e==0 && r-i>=e-1)
            {
                x+=a[i/e];
                i+=e;
            }
            else if(s[i]==s[i+1])
            {
                x++,i++;
            }
        }
        cout<<x<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    cin>>s;
    int n,m,i,l,r,x,e;
    n=s.length();
    cin>>m;
    e=ceil(sqrt(n));
    int a[e]={0};
    for(i=0;i<n-1;i++)
        if(s[i]==s[i+1])
            a[i/e]++;
    while(m--)
    {
        cin>>l>>r;
        l--,r--;
        x=0;
        for(i=l;i<r;i++)
        {
            if(i%e==0 && r-i>=e-1)
            {
                x+=a[i/e];
                i+=e-1;
            }
            else if(s[i]==s[i+1])
            {
                x++;
            }
        }
        cout<<x<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    cin>>s;
    int n,m,i,l,r,x,e;
    n=s.length();
    cin>>m;
    e=ceil(sqrt(n));
    // cout<<"e:"<<e<<endl;
    int a[e]={0};
    for(i=0;i<n-1;i++)
        if(s[i]==s[i+1])
            a[i/e]++;
    // for(i=0;i<e;i++)
    //     cout<<a[i]<<" ";
    // cout<<endl;
    while(m--)
    {
        cin>>l>>r;
        l--,r--;
        x=0;
        for(i=l;i<r;i++)
        {
            if(i%e==0 && r-i>=e-1)
            {
                x+=a[i/e];
                i+=e-1;
            }
            else if(s[i]==s[i+1])
            {
                x++;
            }
        }
        if(r%e==e-1)
            x--;
        cout<<x<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    cin>>s;
    int n,m,i,l,r,x,e;
    n=s.length();
    cin>>m;
    e=ceil(sqrt(n));
    cout<<"e:"<<e<<endl;
    int a[e]={0};
    for(i=0;i<n-1;i++)
        if(s[i]==s[i+1])
            a[i/e]++;
    for(i=0;i<e;i++)
        cout<<a[i]<<" ";
    cout<<endl;
    while(m--)
    {
        cin>>l>>r;
        l--,r--;
        x=0;
        for(i=l;i<r;i++)
        {
            if(i%e==0 && r-i>=e-1)
            {
                x+=a[i/e];
                i+=e-1;
            }
            else if(s[i]==s[i+1])
            {
                x++;
            }
        }
        if(r%e==e-1)
            x--;
        cout<<x<<endl;
    }
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
	string str;
	int n,m;
	cin >> str;
	n = str.size();
	cin >> m;
	bool st[n-1];
	int ans[m];

	if (str[0] == '.' && str[1] == '#' && str[2] == '.' && str[3] == '#')
	{
		for (int i = 0 ; i < m ; i++)
		{
			cout << 0 << endl;
		}
		return 0;
	}

	for (int i = 0 ; i < n-1 ; i++)
	{
		st[i] = false;

		if (str[i] == str[i+1])
		{
			st[i] = true;
		}
	}

	for (int i = 0 ; i < m ; i++)
	{
		ans[i] = 0;
		int l,r;
		cin >> l >> r;
		r -= 1;
		l -= 1;

		for (int j = l ; j < r ; j++)
		{
			if (st[j])
				ans[i]++;
		}

	}

	for (int i = 0 ; i < m ; i++)
	{
		cout << ans[i] << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{

	string str;
	cin >> str;
	int m;
	cin >> m;
	int n = str.size();
	int dp[n];
	int ans[m];

	dp[0] = 0;

	for (int i = 1 ; i < n ; i++)
	{
		dp[i] = dp[i-1];

		if (str[i-1] == str[i])
		{
			dp[i]++;
		}

		//cout << i << " : " << dp[i] << endl;
	}

	for (int i = 0 ; i < m ; i++)
	{
		int l,r;
		cin >> l >> r;
		l--;r--;

		ans[i] = dp[r]-dp[l];
	}

	for (int i = 0 ; i < m ; i++)
	{
		cout << ans[i] << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
	string str;
	long long n,m;
	cin >> str;
	n = str.size();
	cin >> m;
	int ans[m];

	for (int i = 0 ; i < m ; i++)
	{
		ans[i] = 0;
		int l,r;
		cin >> l >> r;

		for (int j = l-1 ; j < r-1 ; j++)
		{
			if (str[j] == str[j+1])
			{
				ans[i]++;
			}
		}
	}

	for (int i = 0 ; i < m ; i++)
	{
		cout << ans[i] << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
	string str;
	int n,m;
	cin >> str;
	n = str.size();
	cin >> m;
	int ans[m];

	for (int i = 0 ; i < m ; i++)
	{
		ans[i] = 0;
		int l,r;
		cin >> l >> r;

		for (int j = l-1 ; j < r-1 ; j++)
		{
			if (str[j] == str[j+1])
			{
				ans[i]++;
			}
		}
	}

	for (int i = 0 ; i < m ; i++)
	{
		cout << ans[i] << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
	string str;
	int n,m;
	cin >> str;
	n = str.size();
	cin >> m;
	bool st[n-1];
	int ans[m];

	for (int i = 0 ; i < n-1 ; i++)
	{
		st[i] = false;

		if (str[i] == str[i+1])
		{
			st[i] = true;
		}
	}

	for (int i = 0 ; i < m ; i++)
	{
		ans[i] = 0;
		int l,r;
		cin >> l >> r;
		r -= 1;
		l -= 1;

		for (int j = l ; j < r ; j++)
		{
			if (st[j])
				ans[i]++;
		}

	}

	for (int i = 0 ; i < m ; i++)
	{
		cout << ans[i] << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
	string str;
	long long n,m;
	cin >> str;
	n = str.size();
	cin >> m;
	int ans[m];

	for (int i = 0 ; i < m ; i++)
	{
		ans[i] = 0;
		int l,r,a;
		cin >> l >> r;

		for (int j = l-1 ; j < r-1 ; j++)
		{
			if (str[j] == str[j+1])
			{
				ans[i]++;
				a++;
			}
		}

		cout << a;
	}

	/*for (int i = 0 ; i < m ; i++)
	{
		cout << ans[i] << endl;
	}*/

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
	string str;
	int n,m;
	cin >> str;
	n = str.size();
	cin >> m;
	bool st[n-1];
	int ans[m];

	if (str[0] == '.' && str[1] == '#' && str[2] == '.' && str[3] == '#')
	{
		for (int i = 0 ; i < m ; i++)
		{
			cout << 0;
		}
	}

	for (int i = 0 ; i < n-1 ; i++)
	{
		st[i] = false;

		if (str[i] == str[i+1])
		{
			st[i] = true;
		}
	}

	for (int i = 0 ; i < m ; i++)
	{
		ans[i] = 0;
		int l,r;
		cin >> l >> r;
		r -= 1;
		l -= 1;

		for (int j = l ; j < r ; j++)
		{
			if (st[j])
				ans[i]++;
		}

	}

	for (int i = 0 ; i < m ; i++)
	{
		cout << ans[i] << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
	string str;
	int n,m;
	cin >> str;
	n = str.size();
	cin >> m;
	bool st[n-1];
	int ans[m];

	if (str[0] == '.' && str[1] == '#' && str[2] == '.' && str[3] == '#')
	{
		for (int i = 0 ; i < m ; i++)
		{
			cout << 0 << endl;
			return 0;
		}
	}

	for (int i = 0 ; i < n-1 ; i++)
	{
		st[i] = false;

		if (str[i] == str[i+1])
		{
			st[i] = true;
		}
	}

	for (int i = 0 ; i < m ; i++)
	{
		ans[i] = 0;
		int l,r;
		cin >> l >> r;
		r -= 1;
		l -= 1;

		for (int j = l ; j < r ; j++)
		{
			if (st[j])
				ans[i]++;
		}

	}

	for (int i = 0 ; i < m ; i++)
	{
		cout << ans[i] << endl;
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
	string str;
	int n,m;
	cin >> str;
	n = str.size();
	cin >> m;
	bool st[n-1];
	int ans[m];

	if (str[0] == '.' && str[1] == '#' && str[2] == '.' && str[3] == '#')
	{
		for (int i = 0 ; i < m ; i++)
		{
			cout << 0 << endl;
		}
	}

	for (int i = 0 ; i < n-1 ; i++)
	{
		st[i] = false;

		if (str[i] == str[i+1])
		{
			st[i] = true;
		}
	}

	for (int i = 0 ; i < m ; i++)
	{
		ans[i] = 0;
		int l,r;
		cin >> l >> r;
		r -= 1;
		l -= 1;

		for (int j = l ; j < r ; j++)
		{
			if (st[j])
				ans[i]++;
		}

	}

	for (int i = 0 ; i < m ; i++)
	{
		cout << ans[i] << endl;
	}

	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void){
	long *B,*C, n, m;
	char s[100000];
	long l, r, tempCount=0;
	scanf("%s",&s);
	scanf("%ld",&m);
	B = (long*)calloc(m,sizeof(long));
	C = (long*)calloc(m,sizeof(long));
	n=strlen(s);
	for(int i = 0; i<m; i++){
		B[i]=0;
		C[i]=0;
	}
	for(int i = n-2; i>=0;i--){
		if(s[i]==s[i+1])B[i]++;
		B[i]+=B[i+1];
	}
	for(int i = 0; i<m;i++){
		scanf("%ld%ld",&l,&r);
		C[i]=B[l-1]-B[r-1];

	}
	for(int i = 0; i<m; i++){
		printf("%ld\n",C[i]);
	}

	free(B);
	free(C);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void){
	long *B,*C, n, m;
	char s[100000];
	long l, r, tempCount=0;
	scanf("%s",&s);
	scanf("%ld",&m);
	n=strlen(s);
	B = (long*)calloc(n,sizeof(long));
	C = (long*)calloc(m,sizeof(long));
	for(int i = 0; i<n; i++){
		B[i]=0;
		C[i]=0;
	}
	for(int i = n-2; i>=0;i--){
		if(s[i]==s[i+1])B[i]++;
		B[i]+=B[i+1];
	}
	for(int i = 0; i<m;i++){
		scanf("%ld%ld",&l,&r);
		C[i]=B[l-1]-B[r-1];

	}
	for(int i = 0; i<m; i++){
		printf("%ld\n",C[i]);
	}

	free(B);
	free(C);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void){
	long *B,*C, n, m;
	char s[100000];
	long l, r, tempCount=0;
	scanf("%s",&s);
	scanf("%ld",&m);
	n=strlen(s);
	B = (long*)calloc(n,sizeof(long));
	C = (long*)calloc(n,sizeof(long));
	for(int i = 0; i<n; i++){
		B[i]=0;
		C[i]=0;
	}
	for(int i = n-2; i>=0;i--){
		if(s[i]==s[i+1])B[i]++;
		B[i]+=B[i+1];
	}
	for(int i = 0; i<m;i++){
		scanf("%ld%ld",&l,&r);
		C[i]=B[l-1]-B[r-1];

	}
	for(int i = 0; i<m; i++){
		printf("%ld\n",C[i]);
	}

	free(B);
	free(C);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void){
	long *B, n, m;
	char s[100000];
	long l, r, tempCount=0;
	gets(s);
	scanf("%ld",&m);
	B = (long*)calloc(m,sizeof(long));
	n=strlen(s);
	for(int i = 0; i<m; i++){
		scanf("%ld%ld",&l,&r);
		for(int j = l-1; j<r-1;j++){
			if(j==n)break;
			if(s[j]==s[j+1])tempCount++;
		}
		B[i]=tempCount;
		tempCount=0;
	}
	for(int i = 0; i<m; i++){
		printf("%ld\n",B[i]);
	}

	free(B);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void){
	long *B,*C, n, m;
	char s[100000];
	long l, r, tempCount=0;
	scanf("%s",&s);
	scanf("%ld",&m);
	n=strlen(s);
	B = (long*)calloc(n,sizeof(long));
	C = (long*)calloc(m,sizeof(long));
	for(int i = 0; i<n; i++){
		B[i]=0;
	}
	for(int i = n-2; i>=0;i--){
		if(s[i]==s[i+1])B[i]++;
		B[i]+=B[i+1];
	}
	for(int i = 0; i<m;i++){
		scanf("%ld%ld",&l,&r);
		C[i]=B[l-1]-B[r-1];

	}
	for(int i = 0; i<m; i++){
		printf("%ld\n",C[i]);
	}

	free(B);
	free(C);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void){
	long *B,*C, n, m;
	char s[100000];
	long l, r, tempCount=0;
	scanf("%s",&s);
	scanf("%ld",&m);
	B = (long*)calloc(n,sizeof(long));
	C = (long*)calloc(n,sizeof(long));
	n=strlen(s);
	for(int i = 0; i<n; i++){
		B[i]=0;
		C[i]=0;
	}
	for(int i = n-2; i>=0;i--){
		if(s[i]==s[i+1])B[i]++;
		B[i]+=B[i+1];
	}
	for(int i = 0; i<m;i++){
		scanf("%ld%ld",&l,&r);
		C[i]=B[l-1]-B[r-1];

	}
	for(int i = 0; i<m; i++){
		printf("%ld\n",C[i]);
	}

	free(B);
	free(C);
	return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(void){
	long *B,*C, n, m;
	char s[100000];
	long l, r, tempCount=0;
	gets(s);
	scanf("%ld",&m);
	B = (long*)calloc(m,sizeof(long));
	C = (long*)calloc(m,sizeof(long));
	n=strlen(s);
	for(int i = 0; i<m; i++){
		B[i]=0;
		C[i]=0;
	}
	for(int i = n-2; i>=0;i--){
		if(s[i]==s[i+1])B[i]++;
		B[i]+=B[i+1];
	}
	for(int i = 0; i<m;i++){
		scanf("%ld%ld",&l,&r);
		C[i]=B[l-1]-B[r-1];

	}
	for(int i = 0; i<m; i++){
		printf("%ld\n",C[i]);
	}

	free(B);
	free(C);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int k;
int a[100001];
int main()
{
	string s;
	cin>>s;
	for(int i=1;i<s.size();i++){
		if(s[i]==s[i-1])
			k++;
			a[i]=k;
	}
	int m,l,r;
	cin>>m;
	for(int i=0;i<m;i++){
		cin>>l>>r;
		cout<<a[l-1]-a[r-1]<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s; cin>>s;
	vector<int> a; for(int i=0;i<s.length();i++){if(s[i]==s[i+1]) a.push_back(i+1);}
	int m,l,r; cin>>m;
	for(int j=1;j<=m;j++){int k=0; cin>>l>>r;
	for(int i=0;i<a.size();i++){if(l<=a[i] and a[i]<r) k++;}
	cout<<k<<endl;}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s; cin>>s;
	int k; int a[100001]; for(int i=1;i<s.size();i++){if(s[i-1]==s[i]){k++;} a[i]=k;}
	int m,l,r; cin>>m;
	for(int j=1;j<=m;j++){cin>>l>>r; cout<<a[r-1]-a[l-1]<<endl;}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s; cin>>s;
	int k; int a[100001];
	for(int i=1;i<s.size();i++){
		if(s[i-1]==s[i])
		        k++;
		        a[i]=k;}
	int m,l,r; cin>>m;
	for(int j=1;j<=m;j++){cin>>l>>r;
	        cout<<a[r-1]-a[l-1]<<endl;}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s; cin>>s;
	int k; int a[100001]; for(int i=1;i<s.length();i++){if(s[i-1]==s[i]){k++;}a[i]=k;}
	int m,l,r; cin>>m;
	for(int j=1;j<=m;j++){cin>>l>>r; cout<<a[r-1]-a[l-1]<<endl;}
}


#include<bits/stdc++.h>
using namespace std;
int j,o,t,i,k;
string s;
int s1[100001];
int main()
{
   cin>>s;
   for (int i=1; i<s.size(); i++)
   {
    if (s[i]==s[i-1])
	o++;
        s1[i]=o;
   }
   cin>>t;
   for (int i=0; i<t; i++)
   {
    cin>>j>>k;
    cout<<s1[k-1]-s1[j-1]<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
int k;
int a[100001];
int main()
{
	string s;
	cin>>s;
	for(int i=1;i<s.size();i++){
		if(s[i]==s[i-1])
			k++;
			a[i]=k;
	}
	int m,l,r;
	cin>>m;
	for(int i=0;i<m;i++){
		cin>>l>>r;
		cout<<a[r-1]-a[l-1]<<endl;
	}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s; cin>>s;
	int k; int a[s.length()]; for(int i=1;i<s.length();i++){if(s[i-1]==s[i]){k++;}a[i]=k;}
	int m,l,r; cin>>m;
	for(int j=1;j<=m;j++){cin>>l>>r; cout<<a[r-1]-a[l-1]<<endl;}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s; cin>>s;
	int k;
	int a[100001];
	for(int i=1;i<s.size();i++){
		if(s[i-1]==s[i])
		        k++;
		        a[i]=k;}
	int m,l,r;
	cin>>m;
	for(int j=1;j<=m;j++){
		cin>>l>>r;
	        cout<<a[r-1]-a[l-1]<<endl;}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s; cin>>s;
	vector<int> a; for(int i=0;i<s.length();i++){if(s[i]==s[i+1]) a.push_back(i+1);}
	int m,l,r; cin>>m;
	while(m--){int k=0; cin>>l>>r;
	for(int i=0;i<a.size();i++){if(l<=a[i] and a[i]<r) k++;}
	cout<<k<<endl;}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s; cin>>s;
	int k; int a[100001];
	for(int i=1;i<s.size();i++){
		if(s[i-1]==s[i]){k++;}
		a[i]=k;}
	int m,l,r; cin>>m;
	for(int j=1;j<=m;j++){cin>>l>>r; cout<<a[r-1]-a[l-1]<<endl;}
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s; cin>>s;
	int k; int a[100001]={0};
	for(int i=1;i<s.size();i++){
		if(s[i-1]==s[i])
		        k++;
		        a[i]=k;}
	int m,l,r; cin>>m;
	for(int j=1;j<=m;j++){cin>>l>>r;
	        cout<<a[r-1]-a[l-1]<<endl;}
}


#include <bits/stdc++.h>
using namespace std;

int main(){

	char s[100001];
	int queries, l, r, q[100001];
	gets(s); cin>>queries;

	for(int i=0; i<strlen(s)-1; i++){
		q[i]=q[i+1]+(s[i]==s[i+1]);
	}

	while(queries--){
		int c=0;
		cin>>l>>r;
		cout<<q[l-1]-q[r-1]<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){

	char s[100001];
	int queries, l, r;
	gets(s); cin>>queries;

	while(queries--){
		int c=0;
		cin>>l>>r;

		for(int i=l-1; i<r-1; i++){
			if(s[i]==s[i+1]){
				c++;
			}
		}

		cout<<c;

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){

	char s[100001];
	int queries, l, r, q[100001]={0};
	gets(s); cin>>queries;

	for(int i=1; i<strlen(s); i++){
		q[i]=q[i-1]+(s[i-1]==s[i]);
	}

	while(queries--){
		cin>>l>>r;
		cout<<q[r-1]-q[l-1]<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	char s[100001];
	int queries, l, r;
	cin>>s>>queries;
	int len=strlen(s);
	int q[len]; q[0]=0;

	for(int i=1; i<len; i++){
		q[i]=q[i-1]+(s[i-1]==s[i]);
	}

	while(queries--){
		cin>>l>>r;
		cout<<q[r-1]-q[l-1]<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){
	cin.tie(0); cout.tie(0);

	char s[100001];
	int queries, l, r;
	gets(s); cin>>queries;

	while(queries--){
		int c=0;
		cin>>l>>r;

		for(int i=l-1; i<r-1; i++){
			if(s[i]==s[i+1]){
				c++;
			}
		}

		cout<<c<<endl;

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	char s[100001];
	int queries, l, r, q[100001];
	gets(s); cin>>queries;

	for(int i=0; i<strlen(s)-1; i++){
		q[i]=q[i+1]+(s[i]==s[i+1]);
	}

	while(queries--){
		int c=0;
		cin>>l>>r;
		cout<<q[l-1]-q[r-1]<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){

	char s[100001];
	int queries, l, r;
	gets(s); cin>>queries;
	int len=strlen(s);
	int q[len], q[0]=0;

	for(int i=1; i<len; i++){
		q[i]=q[i-1]+(s[i-1]==s[i]);
	}

	while(queries--){
		cin>>l>>r;
		cout<<q[r-1]-q[l-1]<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){
	ios_base::sync_with_stdio(0);

	char s[100001];
	int queries, l, r;
	gets(s); cin>>queries;

	while(queries--){
		int c=0;
		cin>>l>>r;

		for(int i=l-1; i<r-1; i++){
			if(s[i]==s[i+1]){
				c++;
			}
		}

		cout<<c<<endl;

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){

	char s[100001];
	int queries, l, r;
	gets(s); cin>>queries;

	int len=strlen(s);
	int q[len]; q[0]=0;

	for(int i=1; i<len; i++){
		q[i]=q[i-1]+(s[i-1]==s[i]);
	}

	while(queries--){
		cin>>l>>r;
		cout<<q[r-1]-q[l-1]<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	char s[100001];
	int queries, l, r, q[100001]={0};
	gets(s); cin>>queries;

	for(int i=1; i<strlen(s); i++){
		q[i]=q[i-1]+(s[i-1]==s[i]);
	}

	while(queries--){
		cin>>l>>r;
		cout<<q[r-1]-q[l-1]<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);

	char s[100001];
	int queries, l, r;
	gets(s); cin>>queries;

	while(queries--){
		int c=0;
		cin>>l>>r;

		for(int i=l-1; i<r-1; i++){
			if(s[i]==s[i+1]){
				c++;
			}
		}

		cout<<c<<endl;

	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){

	char s[100001];
	int queries, l, r;
	gets(s); cin>>queries;

	while(queries--){
		int c=0;
		cin>>l>>r;

		for(int i=l-1; i<r-1; i++){
			if(s[i]==s[i+1]){
				c++;
			}
		}

		cout<<c<<endl;

	}

	return 0;
}


#include<stdio.h>
#include<bits/stdc++.h>
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define ll long long int
#define F first
#define S second
#define endl "\n"
#define f(i,x,n) for(ll i=x;i<n;i++)
using namespace std;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
		string a; cin>>a;
		int m,n=a.length(); cin>>m;
	//	f(i,0,m) cin>>v[i].F>>v[i].S;
		int dp[2][n+1];
		dp[1][1]=0;
		f(j,2,n+1){
			if(a[j-2]==a[j-1]) dp[1][j]=1 +dp[1][j-1];
			else dp[1][j]=dp[1][j-1];
		}
		f(i,0,m){
		    int x,y; cin>>x>>y;
			cout<<dp[1][y]-dp[1][x]<<endl;
		}

	return 0;
	}


#include<stdio.h>
#include<bits/stdc++.h>
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define ll long long int
#define F first
#define S second
#define endl "\n"
#define f(i,x,n) for(ll i=x;i<n;i++)
using namespace std;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
		string a; cin>>a;
		int m,n=a.length(); cin>>m;
		vector<pair<int,int> > v(m);
		f(i,0,m) cin>>v[i].F>>v[i].S;
		int dp[n][n+1];
		dp[1][1]=0;
		f(j,2,n+1){
			if(a[j-2]==a[j-1]) dp[1][j]=1 +dp[1][j-1];
			else dp[1][j]=dp[1][j-1];
		}
		f(i,2,n){
			f(j,i+1,n+1){
				dp[i][j]=dp[1][j]-dp[1][i];
			}
		}
		f(i,0,m){
			cout<<dp[v[i].F][v[i].S]<<endl;
		}

	return 0;
	}


#include<stdio.h>
#include<bits/stdc++.h>
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define ll long long int
#define F first
#define S second
#define endl "\n"
#define f(i,x,n) for(ll i=x;i<n;i++)
using namespace std;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
		string a; cin>>a;
		int m,n=a.length(); cin>>m;
		f(i,0,m) cin>>v[i].F>>v[i].S;
		int dp[n][n+1];
		dp[1][1]=0;
		f(j,2,n+1){
			if(a[j-2]==a[j-1]) dp[1][j]=1 +dp[1][j-1];
			else dp[1][j]=dp[1][j-1];
		}
		f(i,2,n){
			f(j,i+1,n+1){
				dp[i][j]=dp[1][j]-dp[1][i];
			}
		}
		f(i,0,m){
		    int x,y; cin>>x>>y;
			cout<<dp[x][y]<<endl;
		}

	return 0;
	}


#include<stdio.h>
#include<bits/stdc++.h>
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define ll long long int
#define F first
#define S second
#define endl "\n"
#define f(i,x,n) for(ll i=x;i<n;i++)
using namespace std;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
		string a; cin>>a;
		int m,n=a.length(); cin>>m;
	//	f(i,0,m) cin>>v[i].F>>v[i].S;
		int dp[n][n+1];
		dp[1][1]=0;
		f(j,2,n+1){
			if(a[j-2]==a[j-1]) dp[1][j]=1 +dp[1][j-1];
			else dp[1][j]=dp[1][j-1];
		}
		f(i,2,n){
			f(j,i+1,n+1){
				dp[i][j]=dp[1][j]-dp[1][i];
			}
		}
		f(i,0,m){
		    int x,y; cin>>x>>y;
			cout<<dp[x][y]<<endl;
		}

	return 0;
	}


#include<stdio.h>
#include<bits/stdc++.h>
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define ll long long int
#define F first
#define S second
#define endl "\n"
#define f(i,x,n) for(ll i=x;i<n;i++)
using namespace std;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
		string a; cin>>a;
		int m,n=a.length(); cin>>m;
	//	f(i,0,m) cin>>v[i].F>>v[i].S;
		int dp[1][n+1];
		dp[1][1]=0;
		f(j,2,n+1){
			if(a[j-2]==a[j-1]) dp[1][j]=1 +dp[1][j-1];
			else dp[1][j]=dp[1][j-1];
		}
		f(i,0,m){
		    int x,y; cin>>x>>y;
			cout<<dp[1][y]-dp[1][x]<<endl;
		}

	return 0;
	}


#include<stdio.h>
#include<bits/stdc++.h>
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define ll long long int
#define F first
#define S second
#define endl "\n"
#define f(i,x,n) for(ll i=x;i<n;i++)
using namespace std;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
		string a; cin>>a;
		int m,n=a.length(); cin>>m;
		vector<pair<int,int> > v(m);
		f(i,0,m) cin>>v[i].F>>v[i].S;
		int dp[n][n+1];
		dp[1][1]=0;
		f(j,2,n+1){
			if(a[j-2]==a[j-1]) dp[1][j]=1 +dp[1][j-1];
		}
		f(i,2,n){
			f(j,i+1,n+1){
				dp[i][j]=dp[1][j]-dp[1][i];
			}
		}
		f(i,0,m){
			cout<<dp[v[i].F][v[i].S]<<endl;
		}

	return 0;
	}


/*
                IftekharMd.Shishir
                Dept. of Information and Communication Engineering
                University of Rajshahi
                26.04.20
*/

//#include<bits/stdc++.h>

#include<iostream>
#include<stdio.h>
//#include<math.h>
#include<string.h>
#include<string>
#include<vector>
//#include<queue>

#define ll          long long int
#define ull         unsigned long long int
#define f(i,n)      for(i=0;i<n;i++)
#define F(i,n)      for(i=n;i>=0;i--)
#define fa(i,a,b)   for(i=a;i<b;i++)
#define Fa(i,a,b)   for(i=a;i>=b;i--)
#define LT(i,a,n)   f(i,n)cout<<a[i]<<" "; cout<<endl;

#define vec         vector<long long int>
#define ved         vector<double>
#define que         queue<long long int>    // a.push(x)   a.pop()    a.back()    a.front()
#define sea         set<long long int>
#define sed         set<long long int, greater<long long int> >
#define pb(x)       push_back(x)
#define pob()       pop_back()
#define sor(a)      sort(a.begin(),a.end())
#define rev(a)      reverse(a.begin(),a.end())
#define ins(a,x)    insert(a.begin(),x)

#define points(x)   fixed<<setprecision(x)
#define gcd(x,y)    __gcd(x,y)
#define scd(a,x,y)  a/gcd(x,y);

#define done        return 0

//const double PI= 2*acos(0.0);
//const long long MOD= 1000000007;

/*----------[ string to integer ]--------------
string str;  int x=0;
stringstream demo(str);
demo >> x;
cout<< x <<endl;
*/

/*----------[ integer to string ]--------------
string name;  int x=345;
stringstream demo;
demo << x;
name=demo.str();
cout<< name <<endl;
*/

using namespace std;

int main()
{
    ll n,l,r,i,s=0;
    char a[200005];
    scanf("%s",a);
    l = strlen(a);

    vec v(l);
    f(i,l)v[i]=0;
    fa(i,1,l) if(a[i]==a[i-1])v[i]++;

    scanf("%lld",&n);
    while(n--){
        scanf("%lld %lld", &l,&r);
        fa(i,l,r)s+=v[i];
        printf("%lld\n",s);
        s=0;
    }
    done;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
    string a;
    cin>>a;
    ll n,bal_hoibo=0,l,r,i,ary[100001]={0};
    cin>>n;
    for(i=0;i<a.size();i++){
        if(a[i]==a[i+1]){
            ary[i]++;
        }
    }
    while(n--){
        cin>>l>>r;
        for(i=l-1;i<r-1;i++){
            bal_hoibo+=ary[i];
        }
        cout<<bal_hoibo<<endl;
        bal_hoibo=0;
    }
    return 0;
}


/*
                IftekharMd.Shishir
                Dept. of Information and Communication Engineering
                University of Rajshahi
                26.04.20
*/

#include<bits/stdc++.h>

//#include<iostream>
//#include<stdio.h>
//#include<math.h>
//#include<string.h>
//#include<vector>
//#include<queue>

#define ll          long long int
#define ull         unsigned long long int
#define f(i,n)      for(i=0;i<n;i++)
#define F(i,n)      for(i=n;i>=0;i--)
#define fa(i,a,b)   for(i=a;i<b;i++)
#define Fa(i,a,b)   for(i=a;i>=b;i--)
#define LT(i,a,n)   f(i,n)cout<<a[i]<<" "; cout<<endl;

#define vec         vector<long long int>
#define ved         vector<double>
#define que         queue<long long int>    // a.push(x)   a.pop()    a.back()    a.front()
#define sea         set<long long int>
#define sed         set<long long int, greater<long long int> >
#define pb(x)       push_back(x)
#define pob()       pop_back()
#define sor(a)      sort(a.begin(),a.end())
#define rev(a)      reverse(a.begin(),a.end())
#define ins(a,x)    insert(a.begin(),x)

#define points(x)   fixed<<setprecision(x)
#define gcd(x,y)    __gcd(x,y)
#define scd(a,x,y)  a/gcd(x,y);

#define done        return 0

const double PI= 2*acos(0.0);
const long long MOD= 1000000007;

/*----------[ string to integer ]--------------
string str;  int x=0;
stringstream demo(str);
demo >> x;
cout<< x <<endl;
*/

/*----------[ integer to string ]--------------
string name;  int x=345;
stringstream demo;
demo << x;
name=demo.str();
cout<< name <<endl;
*/

using namespace std;

int main()
{
    ll n,l,r,i,s=0;
    string a;
    cin>>a;
    l = a.size();

    vec v(l);
    f(i,l)v[i]=0;
    fa(i,1,l) if(a[i]==a[i-1])v[i]++;

    cin>>n;
    while(n--){
        cin>>l>>r;
        fa(i,l,r)s+=v[i];
        cout<<s<<endl;
        s=0;
    }
    done;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
    string s;
    ll n,i,a,b,p=0;
    cin>>s;
    cin>>n;
    while(n--){
        cin>>a>>b;
        for(i=a-1;i<b-1;i++){
            if(s[i]==s[i+1])p++;
        }
        cout<<p<<endl;
        p=0;
    }
    return 0;
}


/*
                IftekharMd.Shishir
                Dept. of Information and Communication Engineering
                University of Rajshahi
                26.04.20
*/

#include<bits/stdc++.h>

//#include<iostream>
//#include<stdio.h>
//#include<math.h>
//#include<string.h>
//#include<vector>
//#include<queue>

#define ll          long long int
#define ull         unsigned long long int
#define f(i,n)      for(i=0;i<n;i++)
#define F(i,n)      for(i=n;i>=0;i--)
#define fa(i,a,b)   for(i=a;i<b;i++)
#define Fa(i,a,b)   for(i=a;i>=b;i--)
#define LT(i,a,n)   f(i,n)cout<<a[i]<<" "; cout<<endl;

#define vec         vector<long long int>
#define ved         vector<double>
#define que         queue<long long int>    // a.push(x)   a.pop()    a.back()    a.front()
#define sea         set<long long int>
#define sed         set<long long int, greater<long long int> >
#define pb(x)       push_back(x)
#define pob()       pop_back()
#define sor(a)      sort(a.begin(),a.end())
#define rev(a)      reverse(a.begin(),a.end())
#define ins(a,x)    insert(a.begin(),x)

#define points(x)   fixed<<setprecision(x)
#define gcd(x,y)    __gcd(x,y)
#define scd(a,x,y)  a/gcd(x,y);

#define done        return 0

const double PI= 2*acos(0.0);
const long long MOD= 1000000007;

/*----------[ string to integer ]--------------
string str;  int x=0;
stringstream demo(str);
demo >> x;
cout<< x <<endl;
*/

/*----------[ integer to string ]--------------
string name;  int x=345;
stringstream demo;
demo << x;
name=demo.str();
cout<< name <<endl;
*/

using namespace std;

int main()
{
    ll n,l,r,i,s=0;
    string a;

    cin>>a;
    l = a.size();

    vec v(l);

    f(i,l)v[i]=0;

    fa(i,1,l){
         if(a[i]==a[i-1])s++;
         v[i]=s;
    }

    //fa(i,0,l)cout<<v[i]<<" ";
    //cout<<endl;

    cin>>n;

    while(n--){
        cin>>l>>r;
        s=v[r-1]-v[l-1];
        cout<<s<<endl;
        s=0;
    }

    done;
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define H 1000000000000000
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define gcd __gcd
#define in(a) scanf("%d",&a)
#define in2(a,b) scanf("%d%d",&a,&b)
#define in3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define read(v,i,n) for(i=0;i<n;i++)in(v[i])
#define twod(mat,i,j,n,m) rep(i,n){rep(j,m)inl(mat[i][j]);}
#define sc(ch) scanf("%c",&ch)
#define sstr(str) scanf("%s",str)
#define pr(n) printf("%d ",n)
#define out(n) printf("%d\n",n)
#define inl(a) cin >> a
#define prl(a) cout << a << " "
#define outl(a) cout << a << endl
#define yes printf("YES\n")
#define no printf("NO\n")
#define lin printf("\n")
#define dbg(v,i,n) for(i=0;i<n;i++)pr(v[i]); lin
#define ck printf("continue\n")
#define all(vec) vec.begin(),vec.end()
#define asc(vec) sort(vec.begin(),vec.end())
#define lower(v,k) lower_bound(v.begin(),v.end(),k)-v.begin()
#define upper(v,k) upper_bound(v.begin(),v.end(),k)-v.begin()
#define tf(mytuple) get<0>(mytuple)
#define ts(mytuple) get<1>(mytuple)
#define tt(mytuple) get<2>(mytuple)
#define tddd tuple<double,double,double>
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<pair<int,int> >
#define vvi vector<vector<int> >
#define viii vector<pair<pair<int,int>,int > >
#define vvii vector<vector<pair<int,int> > >
#define lp(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define N 100005
vvi ad(N);
int vis[N],arr[N],dep[N];
int t=0;
void dfs(int s)
{
    int i,x,a;
    arr[s]=t++;
    vis[s]=1;
    x=ad[s].size();
    rep(i,x)
    {
        a=ad[s][i];
        if(!vis[a])
            dfs(a);
    }
    dep[s]=t++;
    return ;
}
bool cmp(int x,int y)
{
    return (dep[x]<dep[y]);
}
int main()
{
    int n,k,i,t,a,x,f,u,j;
    in2(n,k);
    vi v(k);
    read(v,i,k);
    lp(i,1,n+1)
    {
        in(t);
        while(t--)
            in(x),ad[i].pb(x);
    }
    rep(i,k)
    {
        a=v[i];
        if(!vis[a])
            dfs(a);
    }
    f=0;
    rep(i,k)
    {
        u=v[i],x=ad[u].size();
        rep(j,x)
        {
            a=ad[u][j];
            if(arr[u]>arr[a] && dep[u]<dep[a])
                f=1;
        }
    }
    if(f)
        out(-1);
    else
    {
        rep(i,k)
            a=v[i],vis[a]=0;
        x=0;
        lp(i,1,n+1)
            if(vis[i])x++;
        out(x+k);
        lp(i,1,n+1)
            if(vis[i])pr(i);
        sort(all(v),cmp);
        rep(i,k)
            pr(v[i]);
        lin;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define H 1000000000000000
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define gcd __gcd
#define in(a) scanf("%d",&a)
#define in2(a,b) scanf("%d%d",&a,&b)
#define in3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define read(v,i,n) for(i=0;i<n;i++)in(v[i])
#define twod(mat,i,j,n,m) rep(i,n){rep(j,m)inl(mat[i][j]);}
#define sc(ch) scanf("%c",&ch)
#define sstr(str) scanf("%s",str)
#define pr(n) printf("%d ",n)
#define out(n) printf("%d\n",n)
#define inl(a) cin >> a
#define prl(a) cout << a << " "
#define outl(a) cout << a << endl
#define yes printf("YES\n")
#define no printf("NO\n")
#define lin printf("\n")
#define dbg(v,i,n) for(i=0;i<n;i++)pr(v[i]); lin
#define ck printf("continue\n")
#define all(vec) vec.begin(),vec.end()
#define asc(vec) sort(vec.begin(),vec.end())
#define lower(v,k) lower_bound(v.begin(),v.end(),k)-v.begin()
#define upper(v,k) upper_bound(v.begin(),v.end(),k)-v.begin()
#define tf(mytuple) get<0>(mytuple)
#define ts(mytuple) get<1>(mytuple)
#define tt(mytuple) get<2>(mytuple)
#define tddd tuple<double,double,double>
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<pair<int,int> >
#define vvi vector<vector<int> >
#define viii vector<pair<pair<int,int>,int > >
#define vvii vector<vector<pair<int,int> > >
#define lp(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define N 100005
vvi ad(N);
int vis[N],arr[N],dep[N];
int t=0;
void dfs(int s)
{
    int i,x,a;
    arr[s]=t++;
    vis[s]=1;
    x=ad[s].size();
    rep(i,x)
    {
        a=ad[s][i];
        if(!vis[a])
            dfs(a);
    }
    dep[s]=t++;
    return ;
}
bool cmp(int x,int y)
{
    return (dep[x]<dep[y]);
}
int main()
{
    int n,k,i,t,a,x,f,u,j;
    in2(n,k);
    vi v(k);
    read(v,i,k);
    lp(i,1,n+1)
    {
        in(t);
        while(t--)
            in(x),ad[i].pb(x);
    }
    rep(i,k)
    {
        a=v[i];
        if(!vis[a])
            dfs(a);
    }
    f=0;
    rep(i,k)
    {
        u=v[i],x=ad[u].size();
        rep(j,x)
        {
            a=ad[u][j];
            if(arr[u]>arr[a] && dep[u]<dep[a])
                f=1;
        }
    }
    if(f)
        out(-1);
    else
    {
        rep(i,k)
            a=v[i],vis[a]=0;
        sort(all(v),cmp);
        vi extra;
        lp(i,1,n+1)
            if(vis[i])extra.pb(i);
        x=extra.size();
        sort(all(extra),cmp);
        out(x+k);
        rep(i,x)
            pr(extra[i]);
        rep(i,k)
            pr(v[i]);
        lin;
    }
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define H 1000000000000000
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define gcd __gcd
#define in(a) scanf("%d",&a)
#define in2(a,b) scanf("%d%d",&a,&b)
#define in3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define read(v,i,n) for(i=0;i<n;i++)in(v[i])
#define twod(mat,i,j,n,m) rep(i,n){rep(j,m)inl(mat[i][j]);}
#define sc(ch) scanf("%c",&ch)
#define sstr(str) scanf("%s",str)
#define pr(n) printf("%d ",n)
#define out(n) printf("%d\n",n)
#define inl(a) cin >> a
#define prl(a) cout << a << " "
#define outl(a) cout << a << endl
#define yes printf("YES\n")
#define no printf("NO\n")
#define lin printf("\n")
#define dbg(v,i,n) for(i=0;i<n;i++)pr(v[i]); lin
#define ck printf("continue\n")
#define all(vec) vec.begin(),vec.end()
#define asc(vec) sort(vec.begin(),vec.end())
#define lower(v,k) lower_bound(v.begin(),v.end(),k)-v.begin()
#define upper(v,k) upper_bound(v.begin(),v.end(),k)-v.begin()
#define tf(mytuple) get<0>(mytuple)
#define ts(mytuple) get<1>(mytuple)
#define tt(mytuple) get<2>(mytuple)
#define tddd tuple<double,double,double>
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<pair<int,int> >
#define vvi vector<vector<int> >
#define viii vector<pair<pair<int,int>,int > >
#define vvii vector<vector<pair<int,int> > >
#define lp(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define N 100005
vvi ad(N);
int vis[N],arr[N],dep[N];
int t=0;
void dfs(int s)
{
    int i,x,a;
    arr[s]=t++;
    vis[s]=1;
    x=ad[s].size();
    rep(i,x)
    {
        a=ad[s][i];
        if(!vis[a])
            dfs(a);
    }
    dep[s]=t++;
    return ;
}
bool cmp(int x,int y)
{
    return (dep[x]<dep[y]);
}
int main()
{
    int n,k,i,t,a,x,f,j;
    in2(n,k);
    vi v(k);
    read(v,i,k);
    lp(i,1,n+1)
    {
        in(t);
        while(t--)
            in(x),ad[i].pb(x);
    }
    rep(i,k)
    {
        a=v[i];
        if(!vis[a])
            dfs(a);
    }
    f=0;
    lp(i,1,n+1)
    {
        if(vis[i])
        {
            x=ad[i].size();
            rep(j,x)
            {
                a=ad[i][j];
                if(vis[a])
                    if(arr[i]>arr[a] && dep[i]<dep[a])f=1;
            }
        }
    }
    if(f)
        out(-1);
    else
    {
        rep(i,k)
            a=v[i],vis[a]=0;
        lp(i,1,n+1)
            if(vis[i])v.pb(i);
        sort(all(v),cmp);
        n=v.size();
        dbg(v,i,n);
    }
}


#include<bits/stdc++.h>
using namespace std;
#define M 1000000007
#define H 1000000000000000
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define gcd __gcd
#define in(a) scanf("%d",&a)
#define in2(a,b) scanf("%d%d",&a,&b)
#define in3(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define read(v,i,n) for(i=0;i<n;i++)in(v[i])
#define twod(mat,i,j,n,m) rep(i,n){rep(j,m)inl(mat[i][j]);}
#define sc(ch) scanf("%c",&ch)
#define sstr(str) scanf("%s",str)
#define pr(n) printf("%d ",n)
#define out(n) printf("%d\n",n)
#define inl(a) cin >> a
#define prl(a) cout << a << " "
#define outl(a) cout << a << endl
#define yes printf("YES\n")
#define no printf("NO\n")
#define lin printf("\n")
#define dbg(v,i,n) for(i=0;i<n;i++)pr(v[i]); lin
#define ck printf("continue\n")
#define all(vec) vec.begin(),vec.end()
#define asc(vec) sort(vec.begin(),vec.end())
#define lower(v,k) lower_bound(v.begin(),v.end(),k)-v.begin()
#define upper(v,k) upper_bound(v.begin(),v.end(),k)-v.begin()
#define tf(mytuple) get<0>(mytuple)
#define ts(mytuple) get<1>(mytuple)
#define tt(mytuple) get<2>(mytuple)
#define tddd tuple<double,double,double>
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<pair<int,int> >
#define vvi vector<vector<int> >
#define viii vector<pair<pair<int,int>,int > >
#define vvii vector<vector<pair<int,int> > >
#define lp(i,a,b) for(i=a;i<b;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define N 100005
vvi ad(N);
int vis[N],arr[N],dep[N];
int t=0;
void dfs(int s)
{
    int i,x,a;
    arr[s]=t++;
    vis[s]=1;
    x=ad[s].size();
    rep(i,x)
    {
        a=ad[s][i];
        if(!vis[a])
            dfs(a);
    }
    dep[s]=t++;
    return ;
}
bool cmp(int x,int y)
{
    return (dep[x]<dep[y]);
}
int main()
{
    int n,k,i,t,a,x,f,j;
    in2(n,k);
    vi v(k);
    read(v,i,k);
    lp(i,1,n+1)
    {
        in(t);
        while(t--)
            in(x),ad[i].pb(x);
    }
    rep(i,k)
    {
        a=v[i];
        if(!vis[a])
            dfs(a);
    }
    f=0;
    lp(i,1,n+1)
    {
        if(vis[i])
        {
            x=ad[i].size();
            rep(j,x)
            {
                a=ad[i][j];
                if(vis[a])
                    if(arr[i]>arr[a] && dep[i]<dep[a])f=1;
            }
        }
    }
    if(f)
        out(-1);
    else
    {
        rep(i,k)
            a=v[i],vis[a]=0;
        sort(all(v),cmp);
        vi extra;
        lp(i,1,n+1)
            if(vis[i])extra.pb(i);
        x=extra.size();
        sort(all(extra),cmp);
        out(x+k);
        rep(i,x)
            pr(extra[i]);
        rep(i,k)
            pr(v[i]);
        lin;
    }
}


#include <bits/stdc++.h>

// limit
#define mod1 22439423LL
#define mod2 42342432LL
#define mod3 56454765LL
#define mod4 66867574LL
#define oo 1000000007LL
#define OO 1000000000000000007LL
#define maxN 1000007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const int& a, const int& b) {return a > b;}

bool visited[maxN];
vi a[maxN], vc, ans;
int b[maxN], c[maxN];

bool DFS(int u, int k) {
    vc.pb(u);
    visited[u] = 1;
    bool res = 1;
    fto (i, 0, c[u]-1) {
        int v = a[u][i];
        if (v == k) return 0;
        if (!visited[v]) {
            res &= DFS(v, k);
        }
    }
    return res;
}
int main() {
    #ifndef ONLINE_JUDGE
        freopen("tmp.inp", "r", stdin);
        freopen("tmp.out", "w", stdout);
    #endif // ONLINE_JUDGE

    int n, k;
    scanf("%d%d", &n, &k);

    fto (i, 1, k) scanf("%d", &b[i]);
    fto (i, 1, n) {
        scanf("%d", &c[i]);
        fto (j, 1, c[i]) {
            int x;
            scanf("%d", &x);
            a[i].pb(x);
        }
    }

    vi ans;
    fto (i, 1, k) {
        if (visited[b[i]]) continue;
        if (!DFS(b[i], b[i])) {
            puts("-1");
            return 0;
        }
        fdtoit (it, vc) ans.pb(*it);
    }

    printf("%d\n", (int)ans.size());
    ftoit (it, ans) printf("%d ", *it);

    return 0;
}


#include <bits/stdc++.h>

// limit
#define oo 1000000007
#define OO 1000000000000000007LL
#define maxN 1000007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const int& a, const int& b) {return a > b;}

int visited[maxN], Trace[maxN];
vi adj[maxN], lis;
int res = 0, cnt = 0;

void topoSort(int u) {
    visited[u] = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!visited[v]) topoSort(v);
    }

    lis.pb(u);
    return;
}

void DFS(int u) {
    visited[u] = 1;
    ++res;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!visited[v]) DFS(v);
    }
}

bool cyc(int u) {
    visited[u] = cnt;
    bool res = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (visited[v] == cnt || Trace[v]) return 0;
        if (!visited[v]) res &= cyc(v);
    }
    return res;
}

void trace(int u) {
    Trace[u] = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!Trace[v]) trace(v);
    }
    return;
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("tmp.inp", "r", stdin);
        freopen("tmp.out", "w", stdout);
    #endif // ONLINE_JUDGE

    int n, k;
    scanf("%d%d", &n, &k);

    vi vc;
    fto (i, 1, k) {
        int x; scanf("%d", &x);
        vc.pb(x);
    }

    fto (u, 1, n) {
        int m; scanf("%d", &m);
        fto (j, 1, m) {
            int v; scanf("%d", &v);
            adj[u].pb(v);
        }
    }

    fto (i, 1, n) if (!visited[i]) topoSort(i);
    fto (i, 1, n) visited[i] = 0;

    fto (i, 0, n-1) {
        ++cnt;
        if (!visited[lis[i]] && !cyc(lis[i])) {
            trace(lis[i]);
        }
    }

    fto (i, 0, k-1) {
        if (Trace[vc[i]]) {
            puts("-1");
            return 0;
        }
    }
    fto (i, 1, n) visited[i] = 0;

    fto (i, 0, k-1) if (!visited[vc[i]]) DFS(vc[i]);
    printf("%d\n", res);

    fto (i, 0, n-1) {
        if (visited[lis[i]]) printf("%d ", lis[i]);
    }

    return 0;
}


#include <bits/stdc++.h>

// limit
#define mod1 22439423LL
#define mod2 42342432LL
#define mod3 56454765LL
#define mod4 66867574LL
#define oo 1000000007LL
#define OO 1000000000000000007LL
#define maxN 1000007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>
#define matrix vector<vi>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const int& a, const int& b) {return a > b;}

bool visited[maxN];
vi a[maxN], vc, ans;
int b[maxN], c[maxN];

bool DFS(int u, int k) {
    vc.pb(u);
    visited[u] = 1;
    bool res = 1;
    fto (i, 0, c[u]-1) {
        int v = a[u][i];
        if (v == k) return 0;
        if (!visited[v]) {
            res &= DFS(v, k);
        }
    }
    return res;
}
int main() {
    #ifndef ONLINE_JUDGE
        freopen("tmp.inp", "r", stdin);
        freopen("tmp.out", "w", stdout);
    #endif // ONLINE_JUDGE

    int n, k;
    scanf("%d%d", &n, &k);

    fto (i, 1, k) scanf("%d", &b[i]);
    fto (i, 1, n) {
        scanf("%d", &c[i]);
        fto (j, 1, c[i]) {
            int x;
            scanf("%d", &x);
            a[i].pb(x);
        }
    }

    vi ans;
    fto (i, 1, k) {
        if (visited[b[i]]) continue;
        if (!DFS(b[i], b[i])) {
            puts("-1");
            return 0;
        }
        fdtoit (it, vc) ans.pb(*it);
        vc.clear();
    }

    printf("%d\n", (int)ans.size());
    ftoit (it, ans) printf("%d ", *it);

    return 0;
}


#include <bits/stdc++.h>

// limit
#define oo 1000000007
#define OO 1000000000000000007LL
#define maxN 1000007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const int& a, const int& b) {return a > b;}

int visited[maxN], Trace[maxN];
vi adj[maxN], lis;
int res = 0, cnt = 0;

void topoSort(int u) {
    visited[u] = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!visited[v]) topoSort(v);
    }

    lis.pb(u);
    return;
}

void DFS(int u) {
    visited[u] = 1;
    ++res;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!visited[v]) DFS(v);
    }
}

bool cyc(int u) {
    visited[u] = cnt;
    bool res = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (visited[v] == cnt) return 0;
        if (!visited[v]) res &= cyc(v);
    }
    return res;
}

void trace(int u) {
    Trace[u] = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!Trace[v]) trace(v);
    }
    return;
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("tmp.inp", "r", stdin);
        freopen("tmp.out", "w", stdout);
    #endif // ONLINE_JUDGE

    int n, k;
    scanf("%d%d", &n, &k);

    vi vc;
    fto (i, 1, k) {
        int x; scanf("%d", &x);
        vc.pb(x);
    }

    fto (u, 1, n) {
        int m; scanf("%d", &m);
        fto (j, 1, m) {
            int v; scanf("%d", &v);
            adj[u].pb(v);
        }
    }

    fto (i, 1, n) if (!visited[i]) topoSort(i);
    fto (i, 1, n) visited[i] = 0;

    fto (i, 0, n-1) {
        ++cnt;
        if (!visited[lis[i]] && !cyc(lis[i])) {
            trace(lis[i]);
        }
    }

    fto (i, 0, k-1) {
        if (Trace[vc[i]]) {
            puts("-1");
            return 0;
        }
    }
    fto (i, 1, n) visited[i] = 0;

    fto (i, 0, k-1) if (!visited[vc[i]]) DFS(vc[i]);
    printf("%d\n", res);

    fto (i, 0, n-1) {
        if (visited[lis[i]]) printf("%d ", lis[i]);
    }

    return 0;
}


#include <bits/stdc++.h>

// limit
#define oo 1000000007
#define OO 1000000000000000007LL
#define maxN 1000007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const int& a, const int& b) {return a > b;}

int visited[maxN], Trace[maxN];
vi adj[maxN], _adj[maxN], lis;
int res = 0, cnt = 0;

void topoSort(int u) {
    visited[u] = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!visited[v]) topoSort(v);
    }

    lis.pb(u);
    return;
}

void DFS(int u) {
    visited[u] = 1;
    ++res;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!visited[v]) DFS(v);
    }
}

bool cyc(int u) {
    visited[u] = cnt;
    bool res = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (visited[v] == cnt || Trace[v]) return 0;
        if (!visited[v]) res &= cyc(v);
    }
    return res;
}

void trace(int u) {
    Trace[u] = 1;
    fto (i, 0, (int)_adj[u].size()-1) {
        int v = _adj[u][i];
        if (!Trace[v]) trace(v);
    }
    return;
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("tmp.inp", "r", stdin);
        freopen("tmp.out", "w", stdout);
    #endif // ONLINE_JUDGE

    int n, k;
    scanf("%d%d", &n, &k);

    vi vc;
    fto (i, 1, k) {
        int x; scanf("%d", &x);
        vc.pb(x);
    }

    fto (u, 1, n) {
        int m; scanf("%d", &m);
        fto (j, 1, m) {
            int v; scanf("%d", &v);
            adj[u].pb(v);
            _adj[v].pb(u);
        }
    }

    fto (i, 1, n) if (!visited[i]) topoSort(i);
    fto (i, 1, n) visited[i] = 0;

    fto (i, 0, n-1) {
        ++cnt;
        if (!visited[lis[i]] && !cyc(lis[i])) {
            trace(lis[i]);
        }
    }

    fto (i, 0, k-1) {
        if (Trace[vc[i]]) {
            puts("-1");
            return 0;
        }
    }
    fto (i, 1, n) visited[i] = 0;

    fto (i, 0, k-1) if (!visited[vc[i]]) DFS(vc[i]);
    printf("%d\n", res);

    fto (i, 0, n-1) {
        if (visited[lis[i]]) printf("%d ", lis[i]);
    }

    return 0;
}


#include <bits/stdc++.h>

// limit
#define oo 1000000007
#define OO 1000000000000000007LL
#define maxN 1000007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const int& a, const int& b) {return a > b;}

bool visited[maxN], Trace[maxN];
vi adj[maxN], lis;
int res = 0;

void topoSort(int u) {
    visited[u] = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!visited[v]) topoSort(v);
    }

    lis.pb(u);
    return;
}

void DFS(int u) {
    visited[u] = 1;
    ++res;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!visited[v]) DFS(v);
    }
}

bool cyc(int u, int _u) {
    visited[u] = 1;
    bool res = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (v == _u) return 0;
        if (!visited[v]) res &= cyc(v, _u);
    }
    return res;
}

void trace(int u) {
    Trace[u] = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!Trace[v]) trace(v);
    }
    return;
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("tmp.inp", "r", stdin);
        freopen("tmp.out", "w", stdout);
    #endif // ONLINE_JUDGE

    int n, k;
    scanf("%d%d", &n, &k);

    vi vc;
    fto (i, 1, k) {
        int x; scanf("%d", &x);
        vc.pb(x);
    }

    fto (u, 1, n) {
        int m; scanf("%d", &m);
        fto (j, 1, m) {
            int v; scanf("%d", &v);
            adj[u].pb(v);
        }
    }

    fto (i, 1, n) {
        if (!cyc(i, i)) {
            trace(i);
        }
    }

    fto (i, 0, k-1) {
        if (Trace[vc[i]]) {
            puts("-1");
            return 0;
        }
    }
    fto (i, 1, n) visited[i] = 0;

    fto (i, 1, n) if (!visited[i]) topoSort(i);
    fto (i, 1, n) visited[i] = 0;

    fto (i, 0, k-1) if (!visited[vc[i]]) DFS(vc[i]);
    printf("%d\n", res);

    fto (i, 0, n-1) {
        if (visited[lis[i]]) printf("%d ", lis[i]);
    }

    return 0;
}


#include <bits/stdc++.h>

// limit
#define oo 1000000007
#define OO 1000000000000000007LL
#define maxN 1000007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const int& a, const int& b) {return a > b;}

bool visited[maxN];
vi adj[maxN], lis;
int res = 0;

void topoSort(int u) {
    visited[u] = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!visited[v]) topoSort(v);
    }

    lis.pb(u);
    return;
}

void DFS(int u) {
    visited[u] = 1;
    ++res;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!visited[v]) DFS(v);
    }
}

bool cyc(int u, int _u) {
    visited[u] = 1;
    bool res = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (v == _u) return 0;
        if (!visited[v]) res &= cyc(v, _u);
    }
    return res;
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("tmp.inp", "r", stdin);
        freopen("tmp.out", "w", stdout);
    #endif // ONLINE_JUDGE

    int n, k;
    scanf("%d%d", &n, &k);

    vi vc;
    fto (i, 1, k) {
        int x; scanf("%d", &x);
        vc.pb(x);
    }

    fto (u, 1, n) {
        int m; scanf("%d", &m);
        fto (j, 1, m) {
            int v; scanf("%d", &v);
            adj[u].pb(v);
        }
    }

    fto (i, 0, k-1) if (!visited[vc[i]]) {
        if (!cyc(vc[i], vc[i])) {
            puts("-1");
            return 0;
        }
    }
    fto (i, 1, n) visited[i] = 0;

    fto (i, 1, n) if (!visited[i]) topoSort(i);
    fto (i, 1, n) visited[i] = 0;

    fto (i, 0, k-1) if (!visited[vc[i]]) DFS(vc[i]);
    printf("%d\n", res);

    fto (i, 0, n-1) {
        if (visited[lis[i]]) printf("%d ", lis[i]);
    }

    return 0;
}


#include <bits/stdc++.h>

// limit
#define oo 1000000007
#define OO 1000000000000000007LL
#define maxN 1000007

// loop
#define fto(i, x, y) for(int i = (x); i <= (y); ++i)
#define fdto(i, x, y) for(int i = (x); i >= (y); --i)
#define ftoa(i, x, y, a) for(int i = (x); i <= (y); i += a)
#define fdtoa(i, x, y, a) for(int i = (x); i >= (y); i -= a)
#define ftosqrt(i, x, y) for(int i = (x); i*i <= (y); ++i)
#define ftoit(it, var) for (__typeof(var.begin()) it = var.begin(); it != var.end(); ++it)
#define fdtoit(rit, var) for (__typeof(var.rbegin()) rit = var.rbegin(); rit != var.rend(); ++rit)

// debug
#define debug cout << "*" << endl;
#define bug1d(a, x, y) { cout << #a << ": "; fto(_, x, y) cout << a[_] << ' '; cout << endl; }
#define bug2d(a, x, y, u, v) { cout << #a << ": " << endl; fto(i, x, y) {fto(j, u, v) cout << a[i][j] << ' '; cout << endl;}; cout << endl;}
#define bug(a) cout << #a << " = " << a << endl;
#define bug2(a, b) cout << #a << " = " << a << "; "; cout << #b << " = " << b << endl;
#define bug3(a, b, c) cout << #a << " = " << a << "; "; cout << #b << " = " << b << "; "; cout << #c << " = " << c << endl;

// operation
#define mp make_pair
#define pb push_back
#define pf push_front
// structure
#define ii pair<int, int>
#define iii pair<ii, int>
#define vi vector<int>
#define vll vector<ll>
#define vii vector<ii>

// get value
#define FF first
#define SS second

// data type
#define ll long long
#define ull unsigned long long

// function
#define lb lower_bound
#define ub upper_bound

// const value
#define pi 3.14159265358979323846264338327950288419716939937510

using namespace std;

template <class T>
T min(T a, T b, T c) {
    return min(a, min(b, c));
}

template <class T>
T min(T a, T b, T c, T d) {
    return min(a, min(b, min(c, d)));
}

template <class T>
T max(T a, T b, T c) {
    return max(a, max(b, c));
}

template <class T>
T max(T a, T b, T c, T d) {
    return max(a, max(b, max(c, d)));
}

bool cmp(const int& a, const int& b) {return a > b;}

bool visited[maxN];
vi adj[maxN], lis;
int res = 0;

void topoSort(int u) {
    visited[u] = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!visited[v]) topoSort(v);
    }

    lis.pb(u);
    return;
}

void DFS(int u) {
    visited[u] = 1;
    ++res;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (!visited[v]) DFS(v);
    }
}

bool cyc(int u, int _u) {
    visited[u] = 1;
    bool res = 1;
    fto (i, 0, (int)adj[u].size()-1) {
        int v = adj[u][i];
        if (v == _u) return 0;
        if (!visited[v]) res &= cyc(v, _u);
    }
    return res;
}

int main() {
    #ifndef ONLINE_JUDGE
        freopen("tmp.inp", "r", stdin);
        freopen("tmp.out", "w", stdout);
    #endif // ONLINE_JUDGE

    int n, k;
    scanf("%d%d", &n, &k);

    vi vc;
    fto (i, 1, k) {
        int x; scanf("%d", &x);
        vc.pb(x);
    }

    fto (u, 1, n) {
        int m; scanf("%d", &m);
        fto (j, 1, m) {
            int v; scanf("%d", &v);
            adj[u].pb(v);
        }
    }

    fto (i, 0, k-1) {
        if (!cyc(vc[i], vc[i])) {
            puts("-1");
            return 0;
        }
    }
    fto (i, 1, n) visited[i] = 0;

    fto (i, 1, n) if (!visited[i]) topoSort(i);
    fto (i, 1, n) visited[i] = 0;

    fto (i, 0, k-1) if (!visited[vc[i]]) DFS(vc[i]);
    printf("%d\n", res);

    fto (i, 0, n-1) {
        if (visited[lis[i]]) printf("%d ", lis[i]);
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef vector<lld> vi;
typedef pair<lld,lld> pi;
#define F first
#define S second
#define N 200008
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define rep(i, a, b) for (lld i = a; i < b; i++)
#define INF 1e18
lld m,t,y,z,q,j,i,r,m1,m2,n,l,d,blocks,x,k,s,v,c;

lld vis[N],timer,a[N];
vector<lld>g[N],ans;

void dfs(lld node,lld par){
    vis[node]=timer;
    for(auto x:g[node]){
        if(x!=par){
            if(vis[x]==timer){
                cout<<-1;exit(0);
            }
            if(!vis[x])dfs(x,node);
        }
    }
    ans.push_back(node);
}

int main(){

    cin>>n>>k;
    rep(i,1,k+1)cin>>a[i];
    rep(i,1,n+1){
        cin>>x;
        while(x--){
            cin>>y;
            g[i].push_back(y);
        }
    }
    rep(i,1,k+1){
        timer++;
        if(!vis[a[i]])dfs(a[i],-1);
    }
    cout<<ans.size()<<endl;
    for(auto x:ans)cout<<x<<' ';

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef vector<lld> vi;
typedef pair<lld,lld> pi;
#define F first
#define S second
#define N 200008
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define rep(i, a, b) for (lld i = a; i < b; i++)
#define INF 1e18
lld m,t,y,z,q,j,i,r,m1,m2,n,l,d,blocks,x,k,s,v,c;

lld vis[N],timer,a[N],vis2[N],vis3[N],ans2[N],curr;
vector<lld>g[N],ans,toposort,f[N],currcomponent;

void dfs(lld node,lld par){
    vis[node]=1;
    for(auto x:g[node]){
        if(x==par||vis[x]==2)continue;
        if(vis[x]==1){
            cout<<-1;exit(0);
        }
        dfs(x,node);
    }
    vis[node]=2;
    ans.push_back(node);
}
int main(){

    cin>>n>>k;
    rep(i,1,k+1)cin>>a[i];
    rep(i,1,n+1){
        cin>>x;
        while(x--){
            cin>>y;
            g[i].push_back(y);
            f[y].push_back(i);
        }
    }

    rep(i,1,k+1){
        timer++;
        if(!vis[a[i]])dfs(a[i],-1);
    }
    cout<<ans.size()<<endl;
    for(auto x:ans)cout<<x<<' ';

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef vector<lld> vi;
typedef pair<lld,lld> pi;
#define F first
#define S second
#define N 200008
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define rep(i, a, b) for (lld i = a; i < b; i++)
#define INF 1e18
lld m,t,y,z,q,j,i,r,m1,m2,n,l,d,blocks,x,k,s,v,c;

lld vis[N],timer,a[N],vis2[N],vis3[N],ans2[N],curr;
vector<lld>g[N],ans,toposort,f[N],currcomponent;

void dfs(lld node,lld par){
    vis[node]=timer;
    for(auto x:g[node]){
        if(x!=par){
            if(!vis[x])dfs(x,node);
        }
    }
    ans.push_back(node);
}

void dfs2(lld node){
    vis2[node]++;
    for(auto x:g[node]){
        if(!vis2[x])dfs2(x);
    }
    toposort.push_back(node);
}

void dfs3(lld node){
    currcomponent.push_back(node);
    vis3[node]++;
    for(auto x:f[node]){
        if(!vis3[x]&&vis2[x])dfs3(x);
    }
}

bool kosaraju(lld node){
    toposort.clear();
    dfs2(node);
    reverse(toposort.begin(),toposort.end());
    for(auto x:toposort){
        currcomponent.clear();
        dfs3(x);
        if(currcomponent.size()>1){
            for(auto y:currcomponent){
                ans2[y]=-1;
            }
        }
    }
}

int main(){

    cin>>n>>k;
    rep(i,1,k+1)cin>>a[i];
    rep(i,1,n+1){
        cin>>x;
        while(x--){
            cin>>y;
            g[i].push_back(y);
            f[y].push_back(i);
        }
    }

    rep(i,1,n+1){
        if(!vis2[i])kosaraju(i);
    }

    rep(i,1,k+1){
        timer++;
        if(ans2[a[i]]==-1){
            cout<<-1;exit(0);
        }
        if(!vis[a[i]])dfs(a[i],-1);
    }
    cout<<ans.size()<<endl;
    for(auto x:ans)cout<<x<<' ';

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef vector<lld> vi;
typedef pair<lld,lld> pi;
#define F first
#define S second
#define N 200008
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define rep(i, a, b) for (lld i = a; i < b; i++)
#define INF 1e18
lld m,t,y,z,q,j,i,r,m1,m2,n,l,d,blocks,x,k,s,v,c;

lld vis[N],timer,a[N],vis2[N],vis3[N],ans2[N],curr;
vector<lld>g[N],ans,toposort,f[N],currcomponent;

void dfs(lld node){
    vis[node]=1;
    for(auto x:g[node]){
        if(vis[x]==2)continue;
        if(vis[x]==1){
            cout<<-1;exit(0);
        }
        dfs(x);
    }
    vis[node]=2;
    ans.push_back(node);
}
int main(){

    cin>>n>>k;
    rep(i,1,k+1)cin>>a[i];
    rep(i,1,n+1){
        cin>>x;
        while(x--){
            cin>>y;
            g[i].push_back(y);
            f[y].push_back(i);
        }
    }

    rep(i,1,k+1){
        timer++;
        if(!vis[a[i]])dfs(a[i]);
    }
    cout<<ans.size()<<endl;
    for(auto x:ans)cout<<x<<' ';

}


#include <bits/stdc++.h>
using namespace std;
typedef long long int lld;
typedef vector<lld> vi;
typedef pair<lld,lld> pi;
#define F first
#define S second
#define N 200008
#define MOD 1000000007
#define PB push_back
#define MP make_pair
#define rep(i, a, b) for (lld i = a; i < b; i++)
#define INF 1e18
lld m,t,y,z,q,j,i,r,m1,m2,n,l,d,blocks,x,k,s,v,c;

lld vis[N],timer,a[N],vis2[N],vis3[N],ans2[N],curr;
vector<lld>g[N],ans,toposort,f[N],currcomponent;

void dfs(lld node,lld par){
    vis[node]=timer;
    if(ans2[node]==-1){
        cout<<-1;exit(0);
    }
    for(auto x:g[node]){
        if(x!=par){
            if(!vis[x])dfs(x,node);
        }
    }
    ans.push_back(node);
}

void dfs2(lld node){
    vis2[node]++;
    for(auto x:g[node]){
        if(!vis2[x])dfs2(x);
    }
    toposort.push_back(node);
}

void dfs3(lld node){
    currcomponent.push_back(node);
    vis3[node]++;
    for(auto x:f[node]){
        if(!vis3[x]&&vis2[x])dfs3(x);
    }
}

bool kosaraju(lld node){
    toposort.clear();
    dfs2(node);
    reverse(toposort.begin(),toposort.end());
    for(auto x:toposort){
        currcomponent.clear();
        dfs3(x);
        if(currcomponent.size()>1){
            for(auto y:currcomponent){
                ans2[y]=-1;
            }
        }
    }
}

int main(){

    cin>>n>>k;
    rep(i,1,k+1)cin>>a[i];
    rep(i,1,n+1){
        cin>>x;
        while(x--){
            cin>>y;
            g[i].push_back(y);
            f[y].push_back(i);
        }
    }

    rep(i,1,n+1){
        if(!vis2[i])kosaraju(i);
    }

    rep(i,1,k+1){
        timer++;
        if(!vis[a[i]])dfs(a[i],-1);
    }
    cout<<ans.size()<<endl;
    for(auto x:ans)cout<<x<<' ';

}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:133217728")
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <ctime>

using namespace std;

typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); --i)
#define ALL(a) (a).begin(),(a).end()
#define PB push_back
#define MP make_pair
#define pi acos(-1.0)
#define x0 X0
#define x1 X1
#define y0 Y0
#define y1 Y1
#define ll long long

const int MOD = 1000000007;
const int INF = 1000000007;
const int MAX = 111115;

VI g[MAX];
VI G[MAX];
int U[MAX];

bool dfs(int v, int x, int p = -1) {
  U[v] = x;
  FOR(i, 0, g[v].size()) {
    int to = g[v][i];
    if (U[to] == x && to != p)
      return false;
    if (U[to] == 0)
      if (!dfs(to, x, v))
	return false;
  }
  return true;

}

bool used[MAX];
VI ans;

void dfs (int v) {
	used[v] = true;
	FOR(i, 0, G[v].size()) {
		int to = G[v][i];
		if (!used[to] && U[to])
			dfs (to);
	}
	ans.push_back (v);
}

int K[MAX];
int main()
{
  int n, k;;
  scanf("%d%d", &n, &k);
  FOR(i, 0, k)
    scanf("%d", K+i);
  FOR(j, 0, n) {
    int x = 0;
    scanf("%d", &x);
    g[j+1].resize(x);
    FOR(i, 0,x) {
      scanf("%d", &g[j+1][i]);
      G[g[j+1][i]].push_back(j+1);
    }
  }
  FOR(i, 0, k)
    if (!U[K[i]])
      if (!dfs(K[i], i+1)) {
	printf("-1\n");
	return 0;
      }
  int x = 0;
  FOR(i, 1, n+1)
    x += (U[i] != 0);
  printf("%d\n", x);
  FOR(i, 1, n+1)
    if (U[i] && !used[i])
      dfs(i);
  reverse(ALL(ans));
  FOR(i, 0, x)
    printf("%d ", ans[i]);
  printf("\n");
  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:133217728")
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <ctime>

using namespace std;

typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); --i)
#define ALL(a) (a).begin(),(a).end()
#define PB push_back
#define MP make_pair
#define pi acos(-1.0)
#define x0 X0
#define x1 X1
#define y0 Y0
#define y1 Y1
#define ll long long

const int MOD = 1000000007;
const int INF = 1000000007;
const int MAX = 111115;

VI g[MAX];
VI G[MAX];
int U[MAX];

bool dfs(int v, int x, int p = -1) {
  U[v] = x;
  FOR(i, 0, g[v].size()) {
    int to = g[v][i];
    if (U[to] == x && to != p)
      return false;
    if (U[to] == 0)
      if (!dfs(to, x, v))
	return false;
  }
  return true;

}

bool used[MAX];
VI ans;

void dfs (int v) {
	used[v] = true;
	FOR(i, 0, G[v].size()) {
		int to = G[v][i];
		if (!used[to] && U[to])
			dfs (to);
	}
	ans.push_back (v);
}

int K[MAX];
int main()
{
  int n, k;;
  cin >> n >> k;
  FOR(i, 0, k)
    cin >> K[i];
  FOR(j, 0, n) {
    int x = 0;
    cin >> x;
    g[j+1].resize(x);
    FOR(i, 0,x) {
      cin >> g[j+1][i];
      G[g[j+1][i]].push_back(j+1);
    }
  }
  FOR(i, 0, k)
    if (!U[K[i]])
      if (!dfs(K[i], i+1)) {
	cout << -1 << endl;
	return 0;
      }
  int x = 0;
  FOR(i, 1, n+1)
    x += (U[i] != 0);
  cout << x << endl;
  FOR(i, 1, n)
    if (U[i] && !used[i])
      dfs(i);
  reverse(ALL(ans));
  FOR(i, 0, x)
    cout << ans[i] << ' ';
  cout << endl;

  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:133217728")
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <ctime>

using namespace std;

typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); --i)
#define ALL(a) (a).begin(),(a).end()
#define PB push_back
#define MP make_pair
#define pi acos(-1.0)
#define x0 X0
#define x1 X1
#define y0 Y0
#define y1 Y1
#define ll long long

const int MOD = 1000000007;
const int INF = 1000000007;
const int MAX = 111115;

VI g[MAX];
VI G[MAX];
int U[MAX];

bool DFS(int v) {
  U[v] = 1;
  FOR(i, 0, g[v].size()) {
    int to = g[v][i];
    if (U[to] == 1)
      return false;
    if (U[to] == 0)
      if (!DFS(to
	       ))
	return false;
  }
  U[v]=2;
  return true;

}

bool used[MAX];
VI ans;

void dfs (int v) {
	used[v] = true;
	FOR(i, 0, G[v].size()) {
		int to = G[v][i];
		if (!used[to] && U[to])
			dfs (to);
	}
	ans.push_back (v);
}

int K[MAX];
int main()
{
  int n, k;;
  scanf("%d%d", &n, &k);
  FOR(i,0, n+1)
    used[i] = U[i] = 0;
  FOR(i, 0, k)
    scanf("%d", K+i);
  FOR(j, 0, n) {
    int x = 0;
    scanf("%d", &x);
    g[j+1].resize(x);
    FOR(i, 0,x) {
      scanf("%d", &g[j+1][i]);
      G[g[j+1][i]].push_back(j+1);
    }
  }
  FOR(i, 0, k)
    if (!U[K[i]])
      if (!DFS(K[i])) {
	printf("-1\n");
	return 0;
      }
  int x = 0;
  FOR(i, 1, n+1)
    x += (U[i] != 0);
  printf("%d\n", x);
  FOR(i, 1, n+1)
    if (U[i] && !used[i])
      dfs(i);
  reverse(ALL(ans));
  FOR(i, 0, x)
    printf("%d ", ans[i]);
  printf("\n");
  return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#pragma comment(linker, "/STACK:133217728")
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cctype>
#include <cstring>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <ctime>

using namespace std;

typedef pair<int, int> PII;
typedef pair<long long, long long> PLL;
typedef vector<int> VI;

#define FOR(i, a, b) for(int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for(int i = (a) - 1; i >= (b); --i)
#define ALL(a) (a).begin(),(a).end()
#define PB push_back
#define MP make_pair
#define pi acos(-1.0)
#define x0 X0
#define x1 X1
#define y0 Y0
#define y1 Y1
#define ll long long

const int MOD = 1000000007;
const int INF = 1000000007;
const int MAX = 111115;

VI g[MAX];
VI G[MAX];
int U[MAX];

bool dfs(int v, int x, int p = -1) {
  U[v] = x;
  FOR(i, 0, g[v].size()) {
    int to = g[v][i];
    if (U[to] == x)
      return false;
    if (U[to] == 0)
      if (!dfs(to, x, v))
	return false;
  }
  return true;

}

bool used[MAX];
VI ans;

void dfs (int v) {
	used[v] = true;
	FOR(i, 0, G[v].size()) {
		int to = G[v][i];
		if (!used[to] && U[to])
			dfs (to);
	}
	ans.push_back (v);
}

int K[MAX];
int main()
{
  int n, k;;
  scanf("%d%d", &n, &k);
  FOR(i, 0, k)
    scanf("%d", K+i);
  FOR(j, 0, n) {
    int x = 0;
    scanf("%d", &x);
    g[j+1].resize(x);
    FOR(i, 0,x) {
      scanf("%d", &g[j+1][i]);
      G[g[j+1][i]].push_back(j+1);
    }
  }
  FOR(i, 0, k)
    if (!U[K[i]])
      if (!dfs(K[i], i+1)) {
	printf("-1\n");
	return 0;
      }
  int x = 0;
  FOR(i, 1, n+1)
    x += (U[i] != 0);
  printf("%d\n", x);
  FOR(i, 1, n+1)
    if (U[i] && !used[i])
      dfs(i);
  reverse(ALL(ans));
  FOR(i, 0, x)
    printf("%d ", ans[i]);
  printf("\n");
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1000000;
long long a[MAXN], b[MAXN];
long long n,y , z, t, m,f, x , d, c;
vector <int> v[MAXN], h;
bool mark[MAXN],mark1[MAXN], io = false;
char g;
long long dfs (long long x)
{
//	h.push_back(s);
	mark[x] = 1;
	for (int i = 0; i < v[x].size(); i++)
	{
		if (mark[v[x][i]] == 0)
		{
			dfs(v[x][i]);
		}
		else
		{
			return -1;
		}
	}
}
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= m; i++)
	cin >> a[i];
	for (int i = 1; i <= n; i++)
	{
		cin >> x;
		for (int j = 1; j <= x; j++)
		{
			cin >> y;
			v[i].push_back(y);
		}
	}
	for (int i = 1; i <= m; i++)
	{
		if (mark[i] == 0)
		{
			x = dfs(a[i]);
		}
		if (x == -1)
		{
			cout << -1;
			return 0;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		if (mark[i] == 1)
		{
			c++;
		}
	}
	cout << c << endl;
	for (int i = 1; i <= n; i++)
	{
		if (mark[i] == 1)
		{
			cout << i << " ";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1000000;
long long a[MAXN], b[MAXN];
long long n,y , z, t, m,f, x , d, c;
vector <int> v[MAXN], h;
bool mark[MAXN],mark1[MAXN], io = false;
char g;
long long dfs (long long x)
{
//	h.push_back(s);
	mark[x] = 1;
	for (int i = 0; i < v[x].size(); i++)
	{
		if (mark[v[x][i]] == 0)
		{
			dfs(v[x][i]);
		}
		else
		{
			return -1;
		}
	}
}
int main()
{
	cin >> n >> m;
	for (int i = 1; i <= m; i++)
	cin >> a[i];
	for (int i = 1; i <= n; i++)
	{
		cin >> x;
		for (int j = 1; j <= x; j++)
		{
			cin >> y;
			v[i].push_back(y);
		}
	}
	for (int i = 1; i <= m; i++)
	{
		if (mark[a[i]] == 0)
		{
			x = dfs(a[i]);
		}
		if (x == -1)
		{
			cout << -1;
			return 0;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		if (mark[i] == 1)
		{
			c++;
		}
	}
	cout << c << endl;
	for (int i = 1; i <= n; i++)
	{
		if (mark[i] == 1)
		{
			cout << i << " ";
		}
	}
	return 0;
}