#include <cstdio>
#include <algorithm>
#include <vector>

using namespace std;

const int di[]={-1, 0, 1, 0}, dj[]={0, -1, 0, 1};

bool is_valid(const vector<size_t> &q, size_t r) {
    for (size_t j=0; j<8; ++j)
        if (j!=r && q[j]==q[r])
            return false;

    for (size_t i=0; i<r; ++i) {
        if (q[i]-i == q[r]-r) return false;
        if (q[i]+i == q[r]+r) return false;
    }

    return true;
}

bool solve_equeen(vector<size_t> &q, size_t r=0) {
    if (r == 8) return true;

    if (!~q[r]) {
        for (size_t c=0; c<8; ++c) {
            q[r] = c;
            if (is_valid(q, r))
                if (solve_equeen(q, r+1))
                    return true;
        }
        q[r] = -1;
    } else {
        if (is_valid(q, r))
            if (solve_equeen(q, r+1))
                return true;
    }

    return false;
}

int main() {
    size_t k;
    scanf("%zu", &k);

    vector<size_t> q(8, -1);
    for (size_t i=0; i<k; ++i) {
        size_t r, c;
        scanf("%zu %zu", &r, &c);

        if (~q[r]) return 1;
        q[r] = c;
    }

    solve_equeen(q);

    for (size_t i=0; i<8; ++i)
        for (size_t j=0; j<8; ++j)
            printf("%c%s", q[i]==j? 'Q':'.', j<7? "":"\n");

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

// macro
#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define all(v) v.begin(), v.end()

// code starts
vector<int> part_ans(0);
vector<int> t_ans;
bool fin=false;
int last=-1;

vector<int> topo_sort1(vector<vector<int>> e)
{
  int n=e.size();
  vector<int> ans(0);
  vector<int> ins(n,0);
  int i,j;
  rep(i,n)
  {
    rep(j,e[i].size())
    {
      ins[e[i][j]]++;
    }
  }
  queue<int> s;
  rep(i,n) if(ins[i]==0)s.push(i);
  while(!s.empty())
  {
    int now=s.front();
    s.pop();
    ans.emplace_back(now);
    rep(i,e[now].size())
    {
      ins[e[now][i]]--;
      if(ins[e[now][i]]==0)
      {
        s.push(e[now][i]);
      }
    }
  }
  return ans;
}

void dfs(int now,vector<bool> &visited,vector<vector<int>> e,int count)
{
  if(fin)return;
  int i,j;
  visited[now]=true;
  part_ans.emplace_back(now);
  int pas=part_ans.size();
  rep(i,e[now].size())
  {
    if(!visited[e[now][i]])
    {
      dfs(e[now][i],visited,e,count+1);
      if(fin)
      {
        return;
      }
    }
    else
    {
      last=e[now][i];
      bool ap=false;
      rep(j,part_ans.size())
      {
        if(!ap)
        {
          if(part_ans[j]==last)
          {
            ap=true;
            t_ans.emplace_back(part_ans[j]);
          }
        }
        else
        {
          t_ans.emplace_back(part_ans[j]);
        }
      }
      fin=true;
      break;
    }
  }
  visited[now]=false;
  part_ans.erase(part_ans.end()-1,part_ans.end());
}

int main()
{
  int n,m;cin>>n>>m;
  vector<int> a(m);
  vector<int> b(m);
  int i,j;
  rep(i,m)cin>>a[i]>>b[i];
  rep(i,m)
  {
    a[i]--;
    b[i]--;
  }
  vector<vector<int>> e(n);
  rep(i,m)
  {
    e[a[i]].emplace_back(b[i]);
  }
  vector<int> ans=topo_sort1(e);
  if(ans.size()==n)
  {
    cout<<-1<<endl;
  }
  else
  {
    rep(i,n)
    {
      vector<bool> visited(n,false);
      dfs(i,visited,e,0);
      if(fin)break;
    }
    while(true)
    {
      vector<bool> inside(n,false);
      rep(i,t_ans.size())
      {
        inside[t_ans[i]]=true;
      }
      vector<int> ins(n,0);
      vector<int> outs(n,0);
      bool ok=true;
      vector<vector<int>> mine(n);
      rep(i,m)
      {
        if(inside[a[i]]&&inside[b[i]])
        {
          ins[b[i]]++;
          outs[a[i]]++;
          mine[a[i]].emplace_back(b[i]);
        }
      }
      int insum=0;
      int outsum=0;
      rep(i,n)
      {
        if(inside[i])
        {
          if(ins[i]!=1||outs[i]!=1)
          {
            ok=false;
          }
        }
      }
      if(ok)break;
      int stat=-1;
      rep(i,t_ans.size())
      {
        if(outs[t_ans[i]]>1)
        {
          stat=i;
          break;
        }
      }
      vector<bool> use(n,true);
      rep(i,outs[t_ans[stat]])
      {        
        vector<bool> vis(n,false);
        int now=t_ans[stat];
        vis[now]=true;
        now=mine[now][i];
        while(true)
        {
          vis[now]=true;
          now=mine[now][0];
          if(vis[now])break;
        }
        rep(j,n)
        {
          if(!vis[j])use[j]=false;
        }
      }
      t_ans.erase(all(t_ans));
      rep(i,n)if(use[i])t_ans.emplace_back(i);
    }
    cout<<t_ans.size()<<endl;
    rep(i,t_ans.size())
    {
      cout<<t_ans[i]+1<<endl;
    }
  }
}