#include<bits/stdc++.h> 
using namespace std;
inline int read(){
	int res=0;
	bool zf=0;
	char c;
	while(((c=getchar())<'0'||c>'9')&&c!='-');
	if(c=='-')zf=1;
	else res=c-'0';
	while((c=getchar())>='0'&&c<='9')res=(res<<3)+(res<<1)+c-'0';
	return (zf?-res:res);
}
inline bool check(int x){
	int tmp=x;
	while(x){
		if(x%10==7)return 0;
		x/=10;
	}
	x=tmp;
	while(x){
		if(x%8==7)return 0;
		x/=8;
	}
	return 1;
}
signed main(){
	int n=read(),ans=0;
	for(register int i=1;i<=n;++i){
		if(check(i))++ans;
	}
	printf("%d\n",ans);
	return 0;
}
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n,tmp;
  int amax=-1;
  int bmin=10000;
  cin>>n;
  for(int i=0;i<n;i++){
    cin>>tmp;
    if(tmp>amax){
      amax=tmp;
    }
  }
  for(int i=0;i<n;i++){
    cin>>tmp;
    if(tmp<bmin){
      bmin=tmp;
    }
  }
  if(bmin<amax){
    cout<<0;
  }else{
    cout<<(bmin-amax+1);
  }
  cout<<endl;
  //cout<<amax<<endl;
  //cout<<bmin<<endl;
}



